【问题标题】:Simple VBA Vlookup Loop with Current Region reference具有当前区域参考的简单 VBA Vlookup 循环
【发布时间】:2015-10-15 13:51:15
【问题描述】:

我的 VB 代码有一列 LookupVals,它成功地查找了列表中的第一个值,但我希望它继续运行并 Vlookup 所有这些值 - 并在下面的单元格中打印结果第一个结果。

这是我目前的编码:

Private Sub CommandButton4_Click()

LookupVal = Sheets("Sheet1").Range("B16").Value
Range("A5").Formula = "=VLOOKUP(" & LookupVal & ",Sheet2!" & Sheets("Sheet2").Range("A4").CurrentRegion.Address & _
",Sheet1!G13,FALSE)"

End Sub

该公式还包含一个保存在单元格(G13,工作表 1)中的 col_index_value 值。

对将它循环到 A6、A7 等有帮助吗?

【问题讨论】:

  • A6 的查找值是多少?不是 B16,对,因为你为什么需要两次这个公式?

标签: vba excel loops vlookup


【解决方案1】:

使用计数器循环行。使用 ("A" & lRow) 而不是硬编码的行 ("A5")。

    Dim ws As Excel.Worksheet
    Dim lRow As Long

    Set ws = ActiveWorkbook.Sheets("Sheet1")
    ws.Activate

    lRow = 5
    Do While lRow <= ws.UsedRange.Rows.count

        'Do your code here.

         LookupVal = Sheets("Sheet1").Range("B16").Value
         ws.Range("A" & lRow).Formula = "=VLOOKUP(" & LookupVal & ",Sheet2!" &    Sheets("Sheet2").Range("A4").CurrentRegion.Address & _
         ",Sheet1!G13,FALSE)"

        lRow = lRow + 1
        ws.Range("A" & lRow).Activate
    Loop

【讨论】:

  • 谢谢马特,但是如何修改它,这样我就不必将 1Row 设置为一个确定的数字?我希望能够为在 lookupvalue 列中具有不同行数的工作表运行相同的宏。而不是数字,我将如何写 1Row = "The numberheld in cell G14" ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
相关资源
最近更新 更多