1

I'm not a VBA expert by any means, but know enough to do the basics. I'm working on a script to help out a local animal shelter. There are four sheets that they've been manually combining into one report. All four reports contain a common variable, which I've named AnimalID. The intent is to find the AnimalID on the Rabies Renewal sheet and return an expiration date to the Report sheet (209 instances as incremented by Count). I've copied the code for one of the selections below. When I attempt to run the script, I receive the following error (through msgbox Err.Description):

Error: "Method 'VLookup' of object 'WorksheetFunction' failed"

AnimalID's are in the first column of the Rabies Renewal sheet and in column Q of the Report. Column K on the Rabies Renewal sheet contains expiration dates, which should copy to column P of the Report. I've tried formatting the AnimalID's as text and numbers but haven't had success.

Dim Count As Long
Dim AnimalID, Renewal As Range

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

Worksheets("Rabies Renewal").Activate
Set Renewal = Sheets("Rabies Renewal").Range("A:AO")

Worksheets("Report").Activate

Range("Q8").Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Count = Selection.Count

For Count = 1 To Count
    AnimalID = "Q" & Count + 7

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

I've also tried a few variants on the syntax, including the lookup value as Range(AnimalID).Value; defining the function as Application.WorksheetFunction.VLookup; table array as a range; and rangelookup as both true and false. Any suggestions would be greatly appreciated. Thank you for your help!

Rich
  • 25
  • 7

2 Answers2

1

This is the expected result for a failed lookup. If have a VLOOKUP() formula in the worksheet like:

enter image description here

You get the #N/A error because 11 does not exist in the little table.

If you run the same thing in VBA:

Sub FailedLookup()
   Dim v As Variant
   v = WorksheetFunction.VLookup(11, Range("A:B"), 2, False)
End Sub

the #N/A does not come through, instead we see:

enter image description here

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
  • 1
    You can get the "#N/A" to come through by changing `WorksheetFunction.VLookup(...` to `Application.VLookup(...` Then one could use the Iferror wrapper to decided what to do in such case. – Scott Craner Jan 10 '16 at 16:45
  • @ScottCraner GREAT COMMENT!!..........Strongly suggest you post an Answer yourself as I see numerous *VBA / VLOOKUP* problem posts. – Gary's Student Jan 10 '16 at 16:52
  • I am away from my computer at the moment and using my phone. Feel free to add to yours, I will not be back for hours. – Scott Craner Jan 10 '16 at 16:54
  • While I agree with your response, the problem is that the values exist in both the Report and Renewal sheets, so I think it's a matter of syntax or variable setup. I just can't nail down what the problem is. I'm able to perform a VLookup within an individual cell but I cannot replicate the behavior through VBA. Thanks though for your input! – Rich Jan 10 '16 at 17:26
  • @Rich Are you trying to lookup the string *Q8* or the contents of cell **Q8** ?? – Gary's Student Jan 10 '16 at 17:34
  • I'd like to lookup the contents of Q8. – Rich Jan 10 '16 at 18:02
1

You need to Set a Range object, not just concatenate a string that looks like the cell address. However, you can use that concatenated string to help define the Range object.

Set AnimalID = Range("Q" & Count + 7)

That will set AnimalID to the Q8 cell of the Report worksheet on the first pass of the For ...Next Statement and cells further down with each subsequent pass.

Here is a wider rewrite of your procedure, removing the necessity to use the .Select and .Activate commands¹.

Dim cnt As Long
Dim AnimalID As Range, Renewal As Range  '<~~declare these PROPERLY!

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

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

    For cnt = 8 To .Cells(Rows.Count, "Q").End(xlUp).Row
        Set AnimalID = .Range("Q" & cnt)
        .Range("P" & cnt) = _
            WorksheetFunction.VLookup(AnimalID, Renewal, 11, False)
    Next Count
End With

¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Community
  • 1
  • 1
  • Hi Jeeped. Thank you so much for your response. I haven't worked much using With, so this was a great learning tool to see how I can simplify my code. Everything seems to be working with these changes. I have to apply it in a similar manner using another sheet in place of Renewal. If I have any problems, I may be back for advice if you don't mind. Thanks a ton! – Rich Jan 10 '16 at 18:54
  • Hi Jeeped. I implemented your suggestions and produced a working result. I'm looking to expand on this and pull information from another sheet based on the AnimalID. I've edited my post with the new full code. The only difference between the two is when to start populating the data in the Report. For Renewal, I want to start in row 8. For Jurisdiction, I want to start in row 140. The code to do this seems to work. Do you see anything obvious I might be missing? Thanks again! – Rich Jan 10 '16 at 21:21
  • @Rich - Could you revert this edit back to the original and start a new post with the new information? While I appreciate your feedback, [SO](http://stackoverflow.com/tour)'s policy is such that when a question has been changed suffciently to invalidate previously valid answers, a new question with the updated information should be posted. I'll try and keep an eye out for it but it would hurt to drop a short note here. The edit you made to your post can be reversed by clicking on the **edited 13 mins ago** then **Rollback** to **1**. –  Jan 10 '16 at 21:27
  • btw, was that row 8 on Renewal or Report; same for row 140, Jurisdiction or Report? –  Jan 10 '16 at 21:34
  • My apologies--I've reverted the post and created a new one. Thanks for the heads up. The row 8 & 140 refer to the Report. I've included this information in the [new post](http://stackoverflow.com/questions/34711609/excel-vba-method-vlookup-of-object-worksheetfunction-failed). – Rich Jan 10 '16 at 22:42