EXCEL-在列表中查找一个值并返回多个相应的值

EXCEL-在列表中查找一个值并返回多个相应的值,excel,indexing,excel-formula,tree-traversal,vba,Excel,Indexing,Excel Formula,Tree Traversal,Vba,我正在尝试在Excel中为我的计划创建一个树遍历。我现在有两个列表,每个1006单元格长。一是前辈,二是后继者。我尝试使用一组函数来显示多个结果。例如,如果输入3,我希望列出任务3的所有后续任务。到目前为止,我提出的代码是: =IF(ISERROR(INDEX($A$1:$B$1006,SMALL(IF($A$1:$A$1006=$E$3,ROW($A$1:$A$1006)),ROW(1:1)),2)),"NO",INDEX($A$1:$B$1006,SMALL(IF($A$1:$A$1006=

我正在尝试在Excel中为我的计划创建一个树遍历。我现在有两个列表,每个1006单元格长。一是前辈,二是后继者。我尝试使用一组函数来显示多个结果。例如,如果输入3,我希望列出任务3的所有后续任务。到目前为止,我提出的代码是:

=IF(ISERROR(INDEX($A$1:$B$1006,SMALL(IF($A$1:$A$1006=$E$3,ROW($A$1:$A$1006)),ROW(1:1)),2)),"NO",INDEX($A$1:$B$1006,SMALL(IF($A$1:$A$1006=$E$3,ROW($A$1:$A$1006)),ROW(1:1)),2))
但是,当我输入前置程序时,它不会显示正确的后续程序

提前感谢您能帮助我的人

您不能用公式连接值,或者至少,我看不到一种简单的方法来连接值

您可以更快但更具侵入性地调用过程:

Option Explicit

Sub Proc_ListPre()
Dim rData As Range, lLastrow As Long, i As Integer
Dim aValues() As Variant
Dim sFilter As String, sRes As String

'Ask for the value to filter to the user
sFilter = InputBox("Which predecessor do you want to analyse?", "Please type the predecessor you want")
If Len(sFilter) = 0 Then Exit Sub

'Define the range
'either use UsedRange (if only columns A and B are used)
'Set rData = ActiveSheet.UsedRange
'or use End(xlUp) if not
lLastrow = ActiveSheet.Range("a65536").End(xlUp).Row
Set rData = ActiveSheet.Range("A1:B" & lLastrow)
'Filter the predecessor with the criteria given in arg
rData.AutoFilter Field:=1, Criteria1:=sFilter

'Find the last row of the filtered data
lLastrow = ActiveSheet.Range("a65536").End(xlUp).Row
aValues = ActiveSheet.Range("A2:B" & lLastrow).Value
'Join the 2nd column of the array
'Join(WorksheetFunction.Index(aValues, 0, 2), ";") 'note that this doesn't work because index returns a 2D array
'Workaround to join the 2nd column
For i = 1 To UBound(aValues, 1)
    If Len(CStr(aValues(i, 2))) > 0 Then
        sRes = sRes & aValues(i, 2) & ";"
    End If
Next
sRes = Left(sRes, Len(sRes) - 1)
MsgBox sRes

ActiveSheet.AutoFilterMode = False
End Sub
或者使用您将在工作表中调用的公式=ListPremypredecessor


您可以添加表格标题和一些示例数据行来说明您的问题吗?谢谢!
Function ListPre(ByVal sFilter As String)
Dim rData As Range, lLastrow As Long, i As Integer
Dim aValues() As Variant
Dim sRes As String

'Define the range
'either use UsedRange (if only columns A and B are used)
'Set rData = ActiveSheet.UsedRange
'or use End(xlUp) if not
lLastrow = ActiveSheet.Range("a65536").End(xlUp).Row
Set rData = ActiveSheet.Range("A1:B" & lLastrow)
aValues = ActiveSheet.Range("A2:B" & lLastrow).Value

'Join the 2nd column of the array
'Join(WorksheetFunction.Index(aValues, 0, 2), ";") 'note that this doesn't work because it returns a 2D array
'Workaround to join the 2nd column
For i = 1 To UBound(aValues, 1)
    If Len(CStr(aValues(i, 2))) > 0 And CStr(aValues(i, 1)) = sFilter Then
        sRes = sRes & aValues(i, 2) & ";"
    End If
Next
sRes = Left(sRes, Len(sRes) - 1)
ListPre = sRes
End Function