簡體   English   中英

跨多個工作表的Vlookup

[英]Vlookup across multiple sheets

這背后的想法是使用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屬性。

請在前進的道路上提出建議。

我想發送文件以進行更好的說明,但似乎找不到附加功能。 謝謝 !

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

我在代碼中解釋了我的答案,但總結了您的問題:

1-您無需定義變量,尤其是工作表。 從不假設您的工作表,並始終定義和設置對工作簿和工作表的引用

2-您將For loops限制為A列的行號和第三行的列號,但是如果它們為空或與您的查詢回合不兼容怎么辦? 然后,您可能會得到錯誤或錯誤的結果。 仔細定義它們。

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

由於您已經完全改變了您要問的內容,因此我將發布另一個答案以使其清楚。 您的要求仍然不十分明確,因此某些輸入可能指向錯誤的目的地,但您可以輕松更改這些輸入。 如果您不了解任何部分,請隨時詢問。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM