Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 同一工作簿内多个工作表上Vlookup的VBA代码_Excel_Vba - Fatal编程技术网

Excel 同一工作簿内多个工作表上Vlookup的VBA代码

Excel 同一工作簿内多个工作表上Vlookup的VBA代码,excel,vba,Excel,Vba,我对编码比较陌生,因此需要一些帮助 我有一份包含多张工作表的工作簿,其中姓名列在a列,地址列在B列 我正在尝试执行以下操作: If worksheets("Input").Range("A1").Value 出现在任何其他工作表(A到Z)上,然后 在匹配的单元格旁边 worksheets("Input").Range("A1").Value i、 e- 有人能解释一下我如何在vba中做到这一点,而不必在每个工作表中使用大量的IFs吗 谢谢 Andrew我用一个小例子测试了它,一张“输入”表和

我对编码比较陌生,因此需要一些帮助

我有一份包含多张工作表的工作簿,其中姓名列在a列,地址列在B列

我正在尝试执行以下操作:

If worksheets("Input").Range("A1").Value
出现在任何其他工作表(A到Z)上,然后

在匹配的单元格旁边

worksheets("Input").Range("A1").Value
i、 e-

有人能解释一下我如何在vba中做到这一点,而不必在每个工作表中使用大量的IFs吗

谢谢


Andrew

我用一个小例子测试了它,一张“输入”表和两张其他表(a和B与您的情况相同)布局是这样的,
vlookup
的循环从输入表a列的第二行开始

Sub foo()

Dim wks As Worksheet
Dim wkb As Workbook
Dim key As String
Dim rowIndex As Integer
Dim wksName As Variant
Dim LastRow As Integer


Set wkb = ThisWorkbook

With wkb.Sheets("Input")
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row ' totol number of entries in col A of input sheet
End With

' for each name in input sheet's column-A search the name in all sheets other then input sheet
For rowIndex = 2 To LastRow Step 1
  key = wkb.Worksheets("Input").Cells(rowIndex, 1).Value ' name to search
    For Each wks In wkb.Worksheets ' loop through all the sheets
        If Not wks.Name = "Input" Then ' avoid searching in input sheet itself
          On Error Resume Next ' to search next sheet when not found in current one
          wkb.Worksheets("Input").Cells(rowIndex, 2).Value = Application.WorksheetFunction.VLookup(key, wks.Range("A:B"), 2, False)
       End If
    Next wks
Next rowIndex

End Sub
更新1>

Sub foo()

Dim wks As Worksheet
Dim wkb As Workbook
Dim key As String
Dim rowIndex As Integer
Dim wksName As Variant
Dim LastRow As Integer


Set wkb = ThisWorkbook



' for name in input sheet's A1 cell, search the name in all sheets other then input sheet

  key = wkb.Worksheets("Input").Range("A1").Value ' name to search
    For Each wks In wkb.Worksheets ' loop through all the sheets
        If Not wks.Name = "Input" Then ' avoid searching in input sheet itself
          On Error Resume Next ' to search next sheet when not found in current one
          wkb.Worksheets("Input").Range("B1").Value = Application.WorksheetFunction.VLookup(key, wks.Range("A:B"), 2, False)
       End If
    Next wks

End Sub

“是否存在于任何其他工作表(A到Z)”您这样说是什么意思?值
工作表(“输入”)。范围(“A1”)。值
是否存在于任何工作表的特定列中,或者您必须在整个工作表中找到它?您还有多少其他工作表?我的意思是你想要寻找值的工作表的数量,因为你的问题有一个循环孔,当你说“在任何其他工作表(a到Z)上存在”时,这包括当前工作表的“输入”总共有27张工作表,第一张工作表是调用输入,我更新了那一张工作表,其余的都是A到Z。谢谢这是一个很好的开始,谢谢。如果我只想从工作表(输入)的一个单元格中搜索值,比如范围(A1),但该值可以更改。您的意思是,在输入工作表(输入工作表的A1单元格)中只有一个条目@AndrewBooth更新1中的代码,在所有工作表中搜索工作表1单元格A1的条目,并将结果粘贴到输入工作表B1中。这是绝对完美的,所有功劳归您所有。非常感谢。
Sub foo()

Dim wks As Worksheet
Dim wkb As Workbook
Dim key As String
Dim rowIndex As Integer
Dim wksName As Variant
Dim LastRow As Integer


Set wkb = ThisWorkbook

With wkb.Sheets("Input")
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row ' totol number of entries in col A of input sheet
End With

' for each name in input sheet's column-A search the name in all sheets other then input sheet
For rowIndex = 2 To LastRow Step 1
  key = wkb.Worksheets("Input").Cells(rowIndex, 1).Value ' name to search
    For Each wks In wkb.Worksheets ' loop through all the sheets
        If Not wks.Name = "Input" Then ' avoid searching in input sheet itself
          On Error Resume Next ' to search next sheet when not found in current one
          wkb.Worksheets("Input").Cells(rowIndex, 2).Value = Application.WorksheetFunction.VLookup(key, wks.Range("A:B"), 2, False)
       End If
    Next wks
Next rowIndex

End Sub
Sub foo()

Dim wks As Worksheet
Dim wkb As Workbook
Dim key As String
Dim rowIndex As Integer
Dim wksName As Variant
Dim LastRow As Integer


Set wkb = ThisWorkbook



' for name in input sheet's A1 cell, search the name in all sheets other then input sheet

  key = wkb.Worksheets("Input").Range("A1").Value ' name to search
    For Each wks In wkb.Worksheets ' loop through all the sheets
        If Not wks.Name = "Input" Then ' avoid searching in input sheet itself
          On Error Resume Next ' to search next sheet when not found in current one
          wkb.Worksheets("Input").Range("B1").Value = Application.WorksheetFunction.VLookup(key, wks.Range("A:B"), 2, False)
       End If
    Next wks

End Sub