Excel VBA"方法' VLookup'对象'工作表功能'失败"

时间:2016-01-10 22:37:44

标签: excel vba excel-vba

这是an earlier post的后续帖子。由于提供了一些很好的建议,这个问题得到了解决。

我通过开发一个脚本将四个工作表的数据合并到一个整体报告中来帮助当地的动物收容所。所有四张表都包含一个公共变量,我将其命名为AnimalID。 AnimalID位于狂犬病更新和狂犬病管辖表的A栏,从最小到最大排序。我之前的问题需要在狂犬病更新表上找到AnimalID,并使用VLookup在报告表上复制到期日期。作为参考,日期填充在报告的开头,在单元格Q8到底部。我试图通过在狂犬病管辖表上找到AnimalID来使用(或者我认为)相同的方法在报告表上填充所有者信息。应该从单元格A140开始填充所有者信息(之前的单元格已经填充),因此我为什么要包含两个变量来标记位置。当我尝试运行脚本时,当它到达任何注释的VLookup行时,我收到以下错误。

  
    

错误:"方法' VLookup'对象'工作表功能'失败"

  

我不确定A ##值是否会正确填充,但我认为其他值应该可以正常工作。将不胜感激的想法和建议。谢谢!

Dim Ct As Long, lMaxRows As Long
Dim AnimalID As Range, Jurisdiction As Range, Renewal As Range

Worksheets("Rabies Renewal").Activate
Range("A:A").NumberFormat = "@"
Set Renewal = Sheets("Rabies Renewal").Range("A:K")

Worksheets("Rabies Jurisdiction").Activate
Range("A:AI").NumberFormat = "@"
Set Jurisdiction = Sheets("Rabies Jurisdiction").Range("A:AI")

Worksheets("Report").Activate
    Range("Q:Q").NumberFormat = "@"

    'Backfill owner information based on animal ID
        lMaxRows = Cells(Rows.Count, "I").End(xlUp).Row
        Range("L" & lMaxRows + 1).Select
        Range(ActiveCell, ActiveCell.End(xlDown)).Select
        Ct = Selection.Count

        For Ct = 1 To Ct
            Set AnimalID = Range("Q" & Ct + lMaxRows)

            'Range("A" & Ct + lMaxRows).Select
            'ActiveCell.Value = WorksheetFunction.VLookup(AnimalID, Jurisdiction, 19 & ", " & 18, False)
            'Range("C" & Ct + lMaxRows).Select
            'ActiveCell.Value = WorksheetFunction.VLookup(AnimalID, Jurisdiction, 27, False)
            'Range("E" & Ct + lMaxRows).Select
            'ActiveCell.Value = WorksheetFunction.VLookup(AnimalID, Jurisdiction, 32, False)
            'Range("F" & Ct + lMaxRows).Select
            'ActiveCell.Value = WorksheetFunction.VLookup(AnimalID, Jurisdiction, 33, False)
            'Range("G" & Ct + lMaxRows).Select
            'ActiveCell.Value = WorksheetFunction.VLookup(AnimalID, Jurisdiction, 34, False)
            'Range("I" & Ct + lMaxRows).Select
            'ActiveCell.Value = WorksheetFunction.VLookup(AnimalID, Jurisdiction, 16, False)
            'Range("J" & Ct + lMaxRows).Select
            'ActiveCell.Value = WorksheetFunction.VLookup(AnimalID, Jurisdiction, 8, False)
            'Range("K" & Ct + lMaxRows).Select
            'ActiveCell.Value = WorksheetFunction.VLookup(AnimalID, Jurisdiction, 11, False)

            On Error Resume Next
        Next Ct

    'Backfill rabies due dates based on animal ID
        Range("Q8").Select
        Range(ActiveCell, ActiveCell.End(xlDown)).Select
        Ct = Selection.Count

        For Ct = 1 To Ct
            Set AnimalID = Range("Q" & Ct + 7)

            Range("P" & Ct + 7).Select
            ActiveCell.Value = WorksheetFunction.VLookup(AnimalID, Renewal, 11, False)

            On Error Resume Next
        Next Ct

    MsgBox "Completed"

1 个答案:

答案 0 :(得分:1)

如果没有样本数据(可以理解的是难以包含140多行),我不得不做出一些假设,但我相信我已经正确地覆盖了报告范围。通过重复点击 F8 键逐步执行此操作。您可以暂停点击并查看工作表,以查看公式是否正确交付a)和b)到正确的范围。

    Dim AnimalID As Range, Jurisdiction As Range, Renewal As Range

    With Worksheets("Rabies Renewal")
        .Range("A:A").NumberFormat = "@"
        Set Renewal = .Range("A:K")
    End With

    With Worksheets("Rabies Jurisdiction")
        .Range("A:AI").NumberFormat = "@"
        Set Jurisdiction = .Range("A:AI")
    End With

    With Worksheets("Report")
        .Range("Q:Q").NumberFormat = "@"

        'Backfill owner information based on animal ID
        With .Range(.Cells(.Cells(Rows.Count, "I").End(xlUp).Row + 1, "L"), _
                    .Cells(.Cells(Rows.Count, "I").End(xlUp).Row + 1, "L").End(xlDown)).Offset(0, -11)
            'should be at A:A parallel to the desired L:L range here
            'A:A - column 19
            .Offset(0, 0).Formula = _
              "=IFERROR(VLOOKUP($Q" & .Rows(1).Row & ", " & Jurisdiction.Address(0, 0, external:=True) & ", 19, FALSE), """")"
            .Cells = .Value
            'C:C - column 27
            .Offset(0, 2).Formula = _
              "=IFERROR(VLOOKUP($Q" & .Rows(1).Row & ", " & Jurisdiction.Address(0, 0, external:=True) & ", 27, FALSE), """")"
            .Cells = .Value
            'E:E - column 32
            .Offset(0, 4).Formula = _
              "=IFERROR(VLOOKUP($Q" & .Rows(1).Row & ", " & Jurisdiction.Address(0, 0, external:=True) & ", 32, FALSE), """")"
            .Cells = .Value
            'F:F - column 33
            .Offset(0, 5).Formula = _
              "=IFERROR(VLOOKUP($Q" & .Rows(1).Row & ", " & Jurisdiction.Address(0, 0, external:=True) & ", 33, FALSE), """")"
            .Cells = .Value
            'G:G - column 34
            .Offset(0, 6).Formula = _
              "=IFERROR(VLOOKUP($Q" & .Rows(1).Row & ", " & Jurisdiction.Address(0, 0, external:=True) & ", 34, FALSE), """")"
            .Cells = .Value
            'I:I - column 16
            .Offset(0, 6).Formula = _
              "=IFERROR(VLOOKUP($Q" & .Rows(1).Row & ", " & Jurisdiction.Address(0, 0, external:=True) & ", 16, FALSE), """")"
            .Cells = .Value
            'J:J - column 8
            .Offset(0, 6).Formula = _
              "=IFERROR(VLOOKUP($Q" & .Rows(1).Row & ", " & Jurisdiction.Address(0, 0, external:=True) & ", 8, FALSE), """")"
            .Cells = .Value
            'K:K - column 11
            .Offset(0, 6).Formula = _
              "=IFERROR(VLOOKUP($Q" & .Rows(1).Row & ", " & Jurisdiction.Address(0, 0, external:=True) & ", 11, FALSE), """")"
            .Cells = .Value
        End With

        'Backfill rabies due dates based on animal ID
        .Range("Q8").Select
        With .Range("P8", .Range("Q8").End(xlDown).Offset(0, -1))
            .Formula = "=IFERROR(VLOOKUP(Q8, " & Renewal.Address(0, 0, external:=True) & ", 11, FALSE), """")"
            .Cells = .Value
        End With
    End With

    MsgBox "Completed"

此方法可以避免通过将单元块一次性加载到整个目标范围来循环遍历单元格。它们将调整为新行,就像您循环它们一样。在提供计算值后,公式将恢复为其值。