需要在任何超出工作表1中find值的出现,并为每个工作表返回A:1的值

我需要search所有的工作表中的工作表一栏A的值。行为应该类似于一个CTRL-F查找所有select。 在每个工作表的A:1中是一个名称,如果A列中的值在该工作表中,则将返回A:1。 我不需要VLookup或HLookup。 这可能是可行的索引和search组合,但我没有find一个好办法做到这一点。 我知道我需要某种数组search,因为我需要到处search。 我有一个不缩放的解决scheme,在回报上是sl sl不驯的。 这是我目前使用的公式。

列A是粘贴search值的位置。 BZ列或其他需要得到的公式粘贴在前200行,这是允许的search条件的限制。

{=IF(OR($A2<>""),IF(OR($A2=Sheet26!$A$1:SZ$25000),Sheet26!A$1,"Not Found"),"")} 

这是Z列的公式,并且每个具有工作表的列都会更改工作表编号。 我需要调整这个只是在列B中的公式,它返回它find的所有名称的连接值。 有很多问题只处理一个值或一个范围,像这样EXCEL:需要从另一个工作表的单元格范围内find一个值,并从相邻单元格返回值,但没有任何实际的答案是我需要的。

目前我得到的结果是这样的。

 ABCDE ... Star Bob Not Found Ann Not Found Light Bob Jill Not Found Not Found 378 Not Found Jill Not Found Not Found 

我想要的是这个

 AB Star Bob, Ann Light Bob, Jill 378 Jill 

我怎样才能修改我的公式来实现呢?

谢谢

如果你厌倦了公式的方法,这里是一个VBA的方法,应该做你所描述的。

  • 它查看表1中的第1列以获取要search的单词列表
  • 把这个列表读入vba数组(为了速度)
  • 对于列表中的每个项目,search每个工作表以查看项目是否存在
    • 我将每个项目添加到一个字典,然后连接结果与逗号,但你也可以构build一个string飞,存储在数组的第二个“列”
  • 完成之后,我们将结果写回工作表。
  • 它应该能够处理任何合理数量的工作表和search条件
  • 如有必要,您可以限制在每个工作表上search的范围; 排除某些工作表被检索; 看一个单元格中的部分匹配; select一个区分大小写的search; 等等
  • 如果在第一个和最后一个search词之间有空白条目,我排除了search。

 Option Explicit Sub FindAllColA() Dim WB As Workbook, WS As Worksheet Dim WS1 As Worksheet Dim D As Object Dim V Dim R As Range Dim FirstRow As Long, LastRow As Long Dim I As Long Set D = CreateObject("scripting.dictionary") Set WB = ThisWorkbook Set WS1 = WB.Worksheets("Sheet1") With WS1 If .Cells(1, 1) <> "" Then FirstRow = 1 Else FirstRow = .Cells(1, 1).End(xlDown).Row End If LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'V will hold both search terms and the results V = .Range(.Cells(FirstRow, 1), .Cells(LastRow, 1)).Resize(columnsize:=2) End With For I = 1 To UBound(V) If Not V(I, 1) = "" Then D.RemoveAll For Each WS In WB.Worksheets If Not WS.Name = WS1.Name Then With WS If Not .Cells.Find(what:=V(I, 1), LookIn:=xlValues, _ lookat:=xlWhole, MatchCase:=False) Is Nothing Then D.Add .Cells(1, 1).Text, .Cells(1, 1).Text End If End With End If Next WS V(I, 2) = Join(D.Keys, ",") Else V(I, 2) = "" End If Next I With WS1 Set R = .Range(.Cells(FirstRow, 1), .Cells(LastRow, 2)) R.EntireColumn.Clear R = V R.EntireColumn.AutoFit End With End Sub 

另一种方式,将是一个UDF,可以在更广泛的范围内使用,而无需进行任何更改:

 Public Function ValString(search_term As String, cell_string As Variant, ParamArray ignored_sheets()) As Variant Dim x As Variant If TypeOf cell_string Is Range Then cell_string = cell_string.Address If Not TypeOf Evaluate(cell_string) Is Range Then ValString = CVErr(2023) Exit Function ElseIf Range(cell_string).Cells.Count > 1 Then ValString = CVErr(2023) Exit Function End If If IsMissing(ignored_sheets) Then ignored_sheets = Array(Application.Caller.Parent.Name) Else For x = 0 To UBound(ignored_sheets) If TypeOf ignored_sheets(x) Is Range Then ignored_sheets(x) = ignored_sheets(x).Parent.Name ElseIf TypeName(ignored_sheets(x)) = "String" Or IsNumeric(ignored_sheets(x)) Then ignored_sheets(x) = Format(ignored_sheets(x), "@") Else ignored_sheets(x) = "" End If Next End If For Each x In ThisWorkbook.Worksheets If IsError(Application.Match(x.Name, Array(ignored_sheets)(0), 0)) Then If Not x.Cells.Find(search_term, , -4163, 1, , , True) Is Nothing Then ValString = ValString & ", " & x.Range(cell_string).Value2 End If End If Next If Len(ValString) Then ValString = Mid(ValString, 3) Else ValString = CVErr(2042) End If End Function 

把代码放在一个模块中,你可以在你的工作表中像普通公式一样使用它。

例:

 =ValString(A1,"A1") 

或者为你的情况:

 =IFERROR(ValString(A1,"A1"),"Not Found") 

使用: ValString([search_term],[cell_string],{[ignored_sheet1],[ignored_sheet2],...})

  • [search_term]:要查找的string
  • [cell_string]:单元格的地址,如果find的话你想要输出的参考或string
  • [ignored_sheets]: 可选)表格名称作为string或引用给他们你想忽略

如果[ignored_sheets]被忽略,那么你有公式的表单将被忽略。 要在工作簿中包含所有工作表,只需将其设置为""
如果没有发现它将返回#N/A! (这是很好的,你可以捕捉这个设置任何你想要的输出而不改变代码)
如果[cell_string]不是一个地址string和/或去多个单元格,它将返回#REF! [ignored_sheets]作为列表=ValString(A1,"A1",Sheet1!A1,Sheet5!A1)=ValString(A1,"A1","Sheet3","Sheet4","Sheet7","MyWhateverSheetName") 。 如果在ref-way中使用,则可以重命名工作表,也可以在公式中使用。 如果您不想查看汇总表,这很好。 但请记住:如果使用了,配方本身的表格也需要包括在内!

如果您还有任何问题,请询问;)

试试这个UDF

 Function findKeywords(findMe As String) As String findKeywords = "" Dim sheetToSkip As String sheetToSkip = "Sheet1" Dim sht As Worksheet For Each sht In ActiveWorkbook.Sheets If sht.Name <> sheetToSkip And Len(findMe) > 0 Then ' do not look for blank cells ' note: LookAt:=xlWhole ... whole word LookAt:=xlPart ... partial Dim aaa As Range Set aaa = sht.Cells.Find( _ What:=findMe, _ After:=sht.Cells(1, 1), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) If Not aaa Is Nothing Then If Len(findKeywords) = 0 Then findKeywords = sht.Range("a1") Else findKeywords = findKeywords & ", " & sht.Range("a1") End If End If End If Next sht ' If Len(findKeywords) = 0 Then findKeywords = "Not Found" ' uncomment to return "Not Found" if desired ' Debug.Print findKeywords End Function