【问题标题】:Vlookup across multiple sheets跨多个工作表的 Vlookup
【发布时间】:2017-09-21 23:04:36
【问题描述】:

这背后的想法是在 sheet11-13 到 sheet1 的 G:AI 列上使用 vba vlookup。标题在所有工作表的第 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

【问题讨论】:

  • 你说这是你的代码,如果你不知道它是否有效,我们怎么知道?
  • 我认为他试图在他的问题中加入一些幽默@sktneer :)。我不知道他的工作簿,但可能因为他的变量而失踪了。
  • @sktneer@0m3r 嗨,我可以联系你们中的任何人寻求帮助吗?真的需要快速解决方案吗? :)

标签: vba excel vlookup


【解决方案1】:

因为你已经完全改变了你的要求。我发布另一个答案来说明清楚。 您的请求仍不完全清楚,因此某些输入可能会引用错误的目的地,但您可以轻松更改这些输入。 如果您有任何不明白的地方,请随时再次提问。

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

【讨论】:

  • 我已经使用了这些代码并且它正在运行。但是,这些列没有完全填满,因为它停在 X 列。vlookup 函数不会从工作表 11-13 中捕获数据。我得到的只是一个标准化的日期“0/1/1990”。感谢您提供最后一个代码,我确实想从每张纸的第 7 列中捕获数据。
  • 如果它没有捕获所有内容,这是因为 sheet1 的 D 列的非空白行号可能小于其他工作表的行号。您可以更改该部分以使其适合您的工作表。只要我们无法预测和管理您工作簿的每个细节,您就必须尝试找出您想要更改的一些细节。
  • 我可以在这里输入一个lastrow代码吗? lastrow = Cells(Rows.Count, 1).End(xlUp).row 这样它会一直运行到 D 列的最后一行然后移动到 E 列等等?
  • 我放在那里的代码计算了 D 列中的非空白单元格。 (这又是因为您的第一个问题和不清楚的解释:)..)但是当我得到 ws 11-12-13 比 sheet1 D 列所拥有的更多。所以最后一行代码也不起作用。您需要使用 wsNames' A 列的行号我认为。
  • @Carmen 我不在我的电脑里,但我编辑了我的答案。现在它将使用 wsNames(sheet11 12 或 13)的相关列的行号,而不是 sheet1 的 D 列的行号。
【解决方案2】:

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

1-您没有定义变量,尤其是工作表。永远不要假设您的工作表并始终定义和设置对工作簿和工作表的引用

2- 您将 For loops 限制为 A 列的行号和第 3 行的列号,但如果它们为空或与您的查找轮次不兼容怎么办?然后你可能会得到错误或错误的结果。仔细定义它们。

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

【讨论】:

  • 这样定义会更好吗?我对此很陌生,很想得到指导。因此,主要目标是从 3 个单独的工作表到 1 个主工作表中从 G 列到 AI 获取信息。我想从上到下循环整个 vlookup 公式,然后从左到右从 G 列到 AI 列。我的标题停在第 3 行,因此后续可用行是第 4 行。我已经合并了您的解决方案,但在 For_col = 1 To ws.Cells("3", Columns.Count).End(xlToLeft).Column 行当我运行它时显示“需要对象”。
  • @Carmen 你应该看看 VBA 最佳实践,它帮助了我/并且仍然帮助了我很多。[stackoverflow.com/documentation/excel-vba/1107/…
  • 感谢您的建议!一定会看的。但与此同时,还有其他解决当前字符串的建议吗?确实需要一个快速的解决方案。 :)
  • @Carmen,您没有在问题中提及 3 个单独的工作表到 1 个主工作表 之类的内容,因此我的答案只查找“mysheets”工作表并带来Sheet1 的值。对于错误行,这就是我写重要通知的原因。但我现在理解的是,你的循环应该重复到“mysheet”表的列号。所以试试这个For for_col = 1 To ws2.Cells("4", Columns.Count).End(xlToLeft).Column 而不是For for_col = 1 To ws.Cells("3", Columns.Count).End(xlToLeft).Column
  • 啊抱歉,我在某些部分写了 ws 而不是 ws1 和 ws2。这就是你出错的原因。请立即查看已编辑的答案。重要的一点是您的"mysheet" 工作表在4th row 中有完整的列吗?和A column 中的完整行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多