在excel中检索与单个ID关联的多个值

我很好奇,如果有一个更简单的解决scheme,在Excel中检索与单个ID相关联的值。

我已经探索了INDEX解决scheme来查找列表中的多个值,但这不是真正的dynamic,并以垂直顺序给出结果,而不是我所需的水平顺序。 (请参阅下面的结果)

我使用的示例函数是这样的

“= IF(ISERROR(SMALL(IF(IF(ISERROR(SEARCH($ A $ 9 $ A $ 1:$ A $ 7)),FALSE,TRUE),ROW($ A $ 1:$ A $ 7)),ROW($ C $ 1:$ C $ 7))), “”,INDEX($ A $ 1:$ C $ 7,SMALL(IF(IF(ISERROR(SEARCH($ A $ 9 $ A $ 1:$ A $ 7)),FALSE,TRUE ),ROW($ A $ 1:$ A $ 7)),ROW($ C $ 1:$ C $ 7)),3))”

*忽略这个例子的参考。

我有两张工作表,基本上需要从“Numbers Sheet”中检索与单个ID相关的值,并将它们存储在“Master Sheet”中。有关更清晰的解释,请参见下面的图片。 公式需要find与该ID关联的后续号码,并将其放在后续的列上,如下所示。

*注意:任何用户ID都可以申请任意数量的门票,因此它的范围可以从1-100(仅以3为例)

感谢来自excel高手的任何指导。 我能想到的唯一的其他解决scheme是使用vba代码来检索每个值并将其存储在数组中,然后从数组中检索值。 让我知道你的想法!

提前致谢!

主表:

在这里输入图像说明

数字表:

在这里输入图像描述

所需结果:

在这里输入图像描述

将下面的公式放在Master Sheet单元格C2 [1]中

 {=IFERROR(INDEX(Numbers!$A:$C,SMALL(IF(Numbers!$A$1:$A$1000=$A2,ROW(Numbers!$A$1:$A$1000)),INT((COLUMN(A:A)-1)/2)+1),MOD(COLUMN(A:A)-1,2)+2),"")} 

[1]我假设它是第2行,因为你不幸没有显示行号。

公式是一个数组公式。 input到没有大括号的单元格中,并用[Ctrl] + [Shift] + [Enter]确认。 花括号然后会自动出现。

然后根据需要向右和向下填充公式。

你可以试试这个代码

 Sub main() Dim IdRng As Range, cell As Range, filtCell As Range Dim i As Long With Worksheets("Master Sheet") Set IdRng = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants) End With With Worksheets("Numbers") With .Cells(1, 1).CurrentRegion For Each cell In IdRng .AutoFilter field:=1, Criteria1:=cell.value '<--| filter it on current department value If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then For Each filtCell In .Offset(1, 1).Resize(.Rows.Count - 1, 1).SpecialCells(XlCellType.xlCellTypeVisible) cell.End(xlToRight).Offset(, 1).Resize(, 2).value = filtCell.Resize(, 2).value Next filtCell End If Next cell End With .AutoFilterMode = False End With With Worksheets("Master Sheet").Cells(1, 1).CurrentRegion.Rows(1) .Insert With .Offset(-1) .Font.Bold = True .Resize(, 2) = Array("ID", "Name") For i = 1 To .Columns.Count - 2 Step 2 .Offset(, 1 + i).Resize(, 2) = Array("Description " & (i + 1) / 2, "Number " & (i + 1) / 2) Next i End With End With End Sub 

VBA可能是一个更好的途径,使用.Find和.FindNext是我会走的路。

附件是一个通用的FindAll函数,所以你可以查找所有包含相关ID的单元格,然后一次处理一个单元格。

 Function FindAll(What, _ Optional SearchWhat As Variant, _ Optional LookIn, _ Optional LookAt, _ Optional SearchOrder, _ Optional SearchDirection As XlSearchDirection = xlNext, _ Optional MatchCase As Boolean = False, _ Optional MatchByte, _ Optional SearchFormat) As Range 'LookIn can be xlValues or xlFormulas, _ LookAt can be xlWhole or xlPart, _ SearchOrder can be xlByRows or xlByColumns, _ SearchDirection can be xlNext, xlPrevious, _ MatchCase, MatchByte, and SearchFormat can be True or False. _ Before using SearchFormat = True, specify the appropriate settings for the Application.FindFormat _ object; eg Application.FindFormat.NumberFormat = "General;-General;""-""" Dim SrcRange As Range If IsMissing(SearchWhat) Then Set SrcRange = ActiveSheet.UsedRange ElseIf TypeOf SearchWhat Is Range Then Set SrcRange = IIf(SearchWhat.Cells.Count = 1, SearchWhat.Parent.UsedRange, SearchWhat) ElseIf TypeOf SearchWhat Is Worksheet Then Set SrcRange = SearchWhat.UsedRange Else: Set SrcRange = ActiveSheet.UsedRange End If If SrcRange Is Nothing Then Exit Function 'get the first matching cell in the range first With SrcRange.Areas(SrcRange.Areas.Count) Dim FirstCell As Range: Set FirstCell = .Cells(.Cells.Count) End With Dim CurrRange As Range: Set CurrRange = SrcRange.Find(What:=What, After:=FirstCell, LookIn:=LookIn, LookAt:=LookAt, _ SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat) If Not CurrRange Is Nothing Then Set FindAll = CurrRange Do Set CurrRange = SrcRange.Find(What:=What, After:=CurrRange, LookIn:=LookIn, LookAt:=LookAt, _ SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat) If CurrRange Is Nothing Then Exit Do If Application.Intersect(FindAll, CurrRange) Is Nothing Then Set FindAll = Application.Union(FindAll, CurrRange) Else: Exit Do End If Loop End If End Function