如何在VBA上查找整个列而不是单个单元格?

时间:2019-11-21 06:40:33

标签: excel vba vlookup

我有一个类似于以下内容的vlookup vba代码。在Set lookFor = book1.Sheets(1).Cells(2, 6)部分中,我要查找并返回整列的值,直到没有剩余的填充值为止,而不是每次都选择一个单元格。

有什么建议吗?

Sub VlookMultipleWorkbooks()

Dim lookFor As Range
Dim srchRange As Range

Dim book1 As Workbook
Dim book2 As Workbook

Dim book2Name As String
book2Name = "Rates.xlsx"    'modify it as per your requirement

Dim book2NamePath As String
book2NamePath = ThisWorkbook.Path & "\" & book2Name

Set book1 = ThisWorkbook

If IsOpen(book2Name) = False Then Workbooks.Open (book2NamePath)

Set book2 = Workbooks(book2Name)
Set lookFor = book1.Sheets(1).Cells(2, 6) ' value to find
Set srchRange = book2.Sheets(1).Range("A:C")    'source

lookFor.Offset(0, 1).Value = Application.VLookup(lookFor, srchRange, 2, False)

End Sub

2 个答案:

答案 0 :(得分:0)

您的描述不清楚。但我可能了解您想要实现的目标。这是我写的一个示例,适合您的情况。只需将范围存储在数组中,然后对每个元素使用vlookup VBA函数,最后将数组数据放入指定的范围。

 Option Explicit
 Option Base 1

 Sub VlookMultipleWorkbooks()

      ' change this data to yours
       Dim rng1 As Range ' values to find
       Set rng1 = ThisWorkbook.Sheets(1).Range("A1:A1000")

       Dim rng2 As Range ' vlookup range 
       Set rng2 = ThisWorkbook.Sheets(2).Range("A1:B1000")

       Dim rngTarget As Range ' where to put your data
       Set rngTarget = ThisWorkbook.Sheets(1).Range("B1:B1000")

       ' because a lot of data you have
       ' instead inserting FormulaR1C1 into sheet
       ' I prefere use arrays 
       Dim arrRng1 As Variant
           arrRng1 = rng1.Value

       Dim arrRng2 As Variant
           arrRng2 = rng2

       Dim i As Long ' counter
       For i = 1 To UBound(arrRng1)
         arrRng1(i, 1) = Application.VLookup(arrRng1(i, 1), arrRng2, 2, False)
       Next i

       rngTarget = arrRng1

    End Sub

答案 1 :(得分:0)

如果我正确理解您的代码,当前唯一的“问题”是您仅对一个单元执行该问题,但希望对列中的所有单元执行该问题。

所以当你写

Set lookFor = book1.Sheets(1).Cells(2, 6) ' value to find
Set srchRange = book2.Sheets(1).Range("A:C")    'source
lookFor.Offset(0, 1).Value = Application.VLookup(lookFor, srchRange, 2, False)

您必须继续

Set lookFor = book1.Sheets(1).Cells(3, 6) ' value to find
lookFor.Offset(0, 1).Value = Application.VLookup(lookFor, srchRange, 2, False)
Set lookFor = book1.Sheets(1).Cells(4, 6) ' value to find
lookFor.Offset(0, 1).Value = Application.VLookup(lookFor, srchRange, 2, False)
Set lookFor = book1.Sheets(1).Cells(5, 6) ' value to find
lookFor.Offset(0, 1).Value = Application.VLookup(lookFor, srchRange, 2, False)

因此,基本上,您需要的是在r = 2 To <LAST_ROW_NUM>上循环并使用Set lookFor = book1.Sheets(1).Cells(r, 6)

Sub VlookMultipleWorkbooks()

  Dim lookFor As Range
  Dim srchRange As Range

  Dim book1 As Workbook
  Dim book2 As Workbook

  Dim book2Name As String
  book2Name = "Rates.xlsx"    'modify it as per your requirement

  Dim book2NamePath As String
  book2NamePath = ThisWorkbook.Path & "\" & book2Name

  Set book1 = ThisWorkbook

  If IsOpen(book2Name) = False Then Workbooks.Open (book2NamePath)

  Set book2 = Workbooks(book2Name)
  Set srchRange = book2.Sheets(1).Range("A:C")    'source

  Dim r As Long
  Dim lastRow As Long

  ' Gets the row num of the last filled row
  lastRow = book1.Sheets(1).Range("F" & book1.Sheets(1).Rows.Count).End(xlUp).Row

  For r = 2 To lastRow
    Set lookFor = book1.Sheets(1).Cells(r, 6) ' value to find    
    lookFor.Offset(0, 1).Value = Application.VLookup(lookFor, srchRange, 2, False)
  Next r

End Sub

简要说明一下:如果要实时更新而不是一次性填写,则需要将FormulaR1C1属性实际设置为包含英语公式的字符串。通过使用R1C1表示法,您可以一次将VLOOKUP公式分配给整个范围,而不必遍历所有单元格。