【问题标题】:How to do a Vlookup/Index-Match?如何进行 Vlookup/索引匹配?
【发布时间】:2019-01-04 16:19:23
【问题描述】:

我在尝试进行 Vlookup/Index-Match 时收到 1004 错误,或者根本没有结果。

我在 While 循环中尝试了 vlookup 和 Index /match。

到目前为止,我在许多宏中都使用了基本相同的循环,没有任何问题。我尝试调整格式(即使使用格式画家)。

我将查找的值隔离在一个新文件中,并只完成了查找部分。

注意:手动 Vlookup 或 index.Match 有效。

代码:

Sub lookup()

 'Variables for the Vlookups
 Dim newRow As Long
 Dim newCL As String

 newRow = 7

 Dim rn As Range
 Set rn = Worksheets("trysheet").Range("C:C")
 Dim mrange As Range
 Set mrange = Worksheets("trysheet").Range("A:A")

 On Error Resume Next
 'lookups
 Do While Worksheets("test").Cells(newRow, 4).Value <> ""
 newCL = Worksheets("test").Cells(newRow, 4).Value
 Worksheets("test").Cells(newRow, 5) = Application.Index(rn, 
 Application.Match(newCL, mrange, 0))
 newRow = newRow + 1
 Loop

End Sub

我尝试过使用 Application.WorksheetFunction 而不仅仅是 Application。但后来我要么收到

1004 错误 - “无法获取 WorksheetFunction 类的 Match/Vlookup 属性”

应用程序定义错误

编辑:
我使用传统的 vlookup 功能(借助宏录制和修改我的循环)达到了所需的结果:

Sub lookup()

'Variables for the Vlookups
Dim newRow As Long

newRow = 7

Dim rn As Range
Set rn = Worksheets("trysheet").Range("C:C")
Dim mrange As Range
Set mrange = Worksheets("trysheet").Range("A:A")

On Error Resume Next
'
'Get Employee
Do While Worksheets("test").Cells(newRow, 4).Value <> ""
 Worksheets("test").Cells(newRow, 5).FormulaR1C1 = "=VLOOKUP(RC[-1], trysheet!C[-4]:C[-2],3,0)"
 newRow = newRow + 1
Loop

End Sub

我通常会尽量避免使用这种方法并坚持使用 application.WorksheetFunction 或 application.Vlookup(example),因为我发现它更干净并且会自动删除公式,但在这种情况下可以完成工作。

仍然帮助我理解为什么应用方法在这里不起作用,因为我第一次遇到这种情况。

yytsunamiyy 在下面有一些很好的建议,但仍然不完整。

【问题讨论】:

  • myrange 中是否存在 newCL 的值?
  • 嗨,Nathan,是的,newCL 在 mrange 中,实际上在我的情况下,newCL 将采用的所有值都将匹配...

标签: excel vba


【解决方案1】:

您的索引公式缺少列引用,请尝试:

Worksheets("test").Cells(newRow, 5) = Application.Index(rn, Application.Match(newCL, mrange, 0),1)

按照下面的讨论我的解决方案:

您的工作表引用似乎不正确,因此您的范围不正确。最佳做法是使用工作表的代号而不是显示名称来引用工作表。下面是如何执行此操作以及如何在程序中使用它的示例:

Public shTest As Worksheet
Public shTrySheet As Worksheet

Sub run_lookups()

    Call defineVars
    Call lookup_newrow

End Sub

Sub defineVars()

    Dim sh As Worksheet

        For Each sh In ThisWorkbook.Sheets
            Select Case sh.CodeName
                Case "Test"
                    Set shTest = sh
                Case "TrySheet"
                    Set shTrySheet = sh
            End Select
        Next sh

End Sub

Sub lookup_newrow()

    'Variables for the Vlookups
     Dim newRow As Long
     Dim newCL As String

        newRow = 7

    Dim rn As Range
        Set rn = shTrySheet.Range("C:C")
    Dim mrange As Range
        Set mrange = shTrySheet.Range("A:A")

 'On Error Resume Next
 'lookups
    Do While shTest.Cells(newRow, 4).Value <> ""
        newCL = shTest.Cells(newRow, 4).Value
        shTest.Cells(newRow, 5).Value = Application.WorksheetFunction.Index(rn, Application.Match(newCL, mrange, 0), 1)
        newRow = newRow + 1
    Loop

End Sub

更优雅的解决方案是将 R1C1 格式的正确公式简单地写入范围。如果您需要固定值,只需进行复制和粘贴。

Public shTest As Worksheet
Public shTrySheet As Worksheet

Sub run_lookups2()

    Call defineVars2
    Call lookup_values

End Sub

Sub defineVars2()

    Dim sh As Worksheet

        For Each sh In ThisWorkbook.Sheets
            Select Case sh.CodeName
                Case "Test"
                    Set shTest = sh
                Case "TrySheet"
                    Set shTrySheet = sh
            End Select
        Next sh

End Sub

Sub lookup_values()
   Dim rLookupResults As Range
   Dim sName As String

    sName = shTrySheet.Name 'Sheet is referenced through codename, which is unlikely to get changed, but for fomula displayed sheetname is used

    With shTest

        Set rLookupResults = .Range(.Cells(7, 5), .Cells(.Cells(.Rows.Count, 4).End(xlUp).Row, 5))

        'enter LOOKUP() Formula in Lookup Rsult Rang
        rLookupResults.FormulaR1C1 = "=IFERROR(LOOKUP(RC[-1]," & sName & "!C[-4]," & sName & "!C[-2]),"""")"

        'if fixed values are need copy and pastespecial
            rLookupResults.Copy
            rLookupResults.PasteSpecial xlPasteValues
    End With

End Sub

【讨论】:

  • 列是可选的吗?
  • 如果您在电子表格中使用 INDEX() 公式并且您有一个单列范围列是可选的 - 但在这种情况下您可以简单地使用 LOOKUP() 来获得相同的结果。但是,我过去发现有时您必须在使用 application.worksheetformula 时定义通常的可选变量。
  • 大家好,我也试过了,实际上我从 3 列范围开始,并引用了第 3 列作为索引的参数。但由于它不起作用,我切换到一维范围......
  • 嗨,请看我编辑的答案。如果这解决了您的问题,如果您将答案标记为已接受,我会很高兴,如果不能随意详细说明。
  • 嘿,非常感谢,但我恐怕这不起作用,我在这一行收到错误:Set rn = shTrySheet.Range("C:C")
【解决方案2】:

如果您只想将公式转换为值,可以执行以下操作。

Do While Worksheets("test").Cells(newRow, 4).Value <> ""
 Worksheets("test").Cells(newRow, 5).FormulaR1C1 = "=VLOOKUP(RC[-1], trysheet!C[-4]:C[-2],3,0)"
 Worksheets("test").Cells(newRow, 5).Value = Worksheets("test").Cells(newRow, 5).Value
 newRow = newRow + 1
Loop

其他方式

 Sub lookup()

     'Variables for the Vlookups
    Dim newRow As Long
    Dim newCL As String
    Dim Mrange As Range
    Dim rn As Range
    newRow = 7
    Set rn = Worksheets("trysheet").Range("C:C")

    Set Mrange = Worksheets("trysheet").Range("A:C") '<~~ A ~ C

    With Worksheets("test")
       Do While .Cells(newRow, 4).Value <> ""
           newCL = .Cells(newRow, 4)
           .Cells(newRow, 5).Value = WorksheetFunction.VLookup(newCL, Mrange, 3, 0)
        newRow = newRow + 1
       Loop
    End With
End Sub

【讨论】:

  • 第一种方法应该可行。第二个给我应用程序定义的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 2017-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
相关资源
最近更新 更多