Excel VBA“对象”WorksheetFunction“的方法”VLookup“失败”

这是以前的post的后续文章 。 由于提供了一些很好的build议,这个问题已经解决了。

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

错误:“对象'WorksheetFunction'的方法'VLookup'失败”

我不知道A ##值是否会正确填充,但我认为其他人应该工作得很好。 思考和build议,将不胜感激。 谢谢!

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" 

如果没有样本数据(可以理解的是很难包括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" 

此方法通过批量将它们一次加载到整个目标范围中来避免循环单元格。 他们会调整到新的行,就像你循环它们一样。 提供计算值后,公式将被还原为其值。