跨越多张表查找

这个背后的想法是使用vba vlookup在G:AI从sheet11-13到Sheet1。 标题在所有工作表的第3行结束。

我写了如下的代码。 代码停在ws1.Cells(r, c).Value = Application.WorksheetFunction.VLookup(ws1.Cells(r, 1).Value, ws2.Range("A1:AI500"), colnum, False)超出范围,有时甚至

运行时错误“1004”:无法获取WorksheetFunction类的VLookup属性。

请在前进的道路上build议。

我想发出更好的说明文件,但似乎无法find附加function。 谢谢 !

 Sub green_update() Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet Set wb = ThisWorkbook Set ws1 = wb.Sheets("Sheet1") Set ws2 = wb.Sheets("Sheet13") Dim bil As String Dim lastrow As Long Dim for_col As Long, i As Long, r As Long, c As Long, colnum As Long r = 4: c = 7: colnum = 7 'mysheets = "sheet11:sheet12:sheet13" 'i would like to allow vlookup to search through all sheet 11-13 For for_col = 1 To ws2.Cells("4", Columns.Count).End(xlToLeft).column lastrow = ws2.Cells(Rows.Count, "A").End(xlUp).row For i = 1 To lastrow - 3 ws1.Cells(r, c).Value = Application.WorksheetFunction.VLookup(ws1.Cells(r, 1).Value, ws2.Range("A1:AI500"), colnum, False) r = r + 1 Next r = 4 colnum = colnum + 1 c = c + 1 Next End Sub 

正如你完全改变了你所要求的。我发布了另外一个答案来说清楚。 尽pipe如此,您的请求还不完全清楚,因此有些input可能会引用错误的目标,但您可以轻松更改这些目标。 如果你不明白任何部分,请随时再问一次。

 Option Explicit Sub green_update() Application.ScreenUpdating = False Dim zaman As Double zaman = Timer Dim wb As Workbook, ws1 As Worksheet, wsNames as Worksheet Set wb = ThisWorkbook Set ws1 = wb.Sheets("Sheet1") Dim colNo As Long, OthARowNo As Long, sh1ARowNo As Long Dim for_col As Long, i As Long, r As Long, c As Long, colnum As Long r = 4: c = 7: colnum = 7 For Each wsNames In Sheets(Array("sheet11", "sheet12", "sheet13")) colNo = wsNames.Cells("4", Columns.Count).End(xlToLeft).column 'Column numbers are 35 but you are working between G:AI which is 29 columns! OthARowNo = wsNames.Cells(Rows.Count, "A").End(xlUp).row sh1ARowNo = ws1.Cells(Rows.Count, "A").End(xlUp).row For for_col = 7 To colNo 'colNo Is 35 Green columns start at 7th column, totally 29 loop, till 35th one. For i = 1 To sh1ARowNo 'This should run until sh1's row number ws1.Cells(r, c).Value = Application.VLookup(ws1.Cells(r, 1).Value, wsNames.Range("A1:AI" & OthARowNo), colnum, False) If IsError(ws1.Cells(r, c).Value) Then ws1.Cells(r, c).Value = "" End If r = r + 1 Next i r = 4 colnum = colnum + 1 c = c + 1 Next for_col colnum = 7 c = c + 6 'There are 6 columns between AI:AP and BR:BY Next wsNames Application.ScreenUpdating = True MsgBox Format(Timer - zaman, "00.00") & "secs" End Sub 

我在代码中解释了我的答案,但总结一下你的问题:

1-您不定义您的variables,特别是工作表。 永远不要假设你的工作表,并总是定义和设置工作簿和工作表的引用

2-您正在使用第三行的列号和第三行的列号限制您的For loops ,但是如果它们是空的或与您的查找循环不兼容? 那么你可能会得到错误或错误的结果。 仔细定义它们。

 Option Explicit Sub green_update() Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet Set wb = ThisWorkbook Set ws1 = wb.Sheets("Sheet1") 'Change this Sheet1 name with your current Worksheet name Set ws2 = wb.Sheets("mysheets") Dim bil As String 'I don't know where do you use that variable. Dim lastrow As Long 'Prefer to work with Long instead of Integer Dim for_col As Long, i As Long, r As Long, c As Long, colnum As Long r = 4: c = 7: colnum = 7 For for_col = 1 To ws2.Cells("4", Columns.Count).End(xlToLeft).Column 'This is important! Here in this case are you sure you, _ 'you would like to define how many times your For loop will run based on 3rd row? lastrow = ws2.Cells(Rows.Count, "A").End(xlUp).Row 'This is also important! Are you sure you would like to run your For loop_ 'based on the row number of A column? I think you should define it -3 _ 'because you start your lookup from D4 (so first 3 one Is not necessary) For i = 1 To lastrow - 3 ws1.Cells(r, c).Value = WorksheetFunction.VLookup(ws1.Cells(r, 4).Value, ws2.Range("A1:AI500"), colnum, False) r = r + 1 Next r = 4 colnum = colnum + 1 c = c + 1 Next End Sub