vba 跨文件查询

文件夹中一个excel中的内容,怎么查询它在同一文件夹中,其他excel的信息(像在不在其他表,在哪个行,表你名是什么)?

Sub 文件夹内查找()
   Dim path           As String
   Dim FileName       As String
   Dim LastCell       As Range
   Dim Wkb            As Workbook
   Dim WS             As Worksheet
   Dim ThisWB         As String
   n = 1
 sr = InputBox("请输入要查找的内容")
Dim MyDir As String
   MyDir = ThisWorkbook.path & "\"
   ThisWB = ThisWorkbook.Name
  Application.EnableEvents = False
  Application.ScreenUpdating = False
   path = MyDir
   FileName = Dir(path & "\*.xls", vbNormal)
   Do Until FileName = ""
      If FileName <> ThisWB Then
          Set Wkb = Workbooks.Open(FileName:=path & "\" & FileName)
          For Each WS In Wkb.Worksheets
           Set foundcell = WS.Cells.Find(sr, LookAt:=xlWhole)
            If Not foundcell Is Nothing Then
                With ThisWorkbook.Sheets(1)
                .[A1:C1] = Array("单元格地址", "工作表名", "工作簿名")
                n = n + 1
                   .Cells(n, 1) = foundcell.Address(0, 0)
                   .Cells(n, 2) = WS.Name
                   .Cells(n, 3) = Wkb.Name
                
                 End With
             End If
          Next WS
          Wkb.Close False
      End If
      FileName = Dir()
   Loop
  Application.EnableEvents = True
  Application.ScreenUpdating = True
   Set Wkb = Nothing
End Sub

追问

你好 这个文件可以啊 。只是当同一个表中 关键词次出现时 ,能否全都搜索出来。能否帮我完善一下?

追答
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-08-15
最简单的方法是利用VLOOKUP函数,是可以查询其他excel文件的
=VLOOKUP(A1,'F:\Downloads\[测试1.xls]Sheet1'!$A:$C,2,0)
自己改改路径
复杂点的可以用VBA本回答被网友采纳