Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application.WorksheetFunction.VLookup 3

Status
Not open for further replies.

pbt1234

Technical User
Jun 25, 2004
23
0
0
US
I am using Application.WorksheetFunction.VLookup to find inventory values for a specified item. When the item is not in the list I get a cryptic message "unable to get the vlookup property of the WorksheetFunction class". How can I deal with this? I thought I could use ISNULL or " = 0" to find these and insert a "0" for the value before things bombed, but neither works. Any help would be appreciated.

Thanks in advance!
pbt1234
 
set the lookup result to a VARIANT and then TEST the variant using
Code:
Myvar = Application.VLookup(x,y,z,f)
If Not IsError(Myvar) Then

End If

Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
I tried adding what you asked and still get the error message . . . Any more ideas?

Thanks!
pbt1234

Code:
Private Sub Newday_click()
Dim strLookUpValue As String
Dim strAnswer As String
Dim rngLookUpRange As Range
Dim intLookUpRow As Integer
Dim myvar As Long

   Do While intLookUpRow < 138
      Set rngLookUpRange = Worksheets("Inventory").Range("A2:D1000")
      strLookUpValue = Worksheets("OPSC").Range("B" & (4 + (7 * intLookUpRow))).Value
           
      myvar = Application.WorksheetFunction.VLookup(strLookUpValue, rngLookUpRange, 4, False)
        
      If Not IsError(myvar) Then
         Sheets("OPSC").Range("E" & (4 + (7 * intLookUpRow))).Value = _
            (Application.WorksheetFunction.VLookup(strLookUpValue, rngLookUpRange, 4, False)) / 1000
      Else
         Sheets("OPSC").Range("E" & (4 + (7 * intLookUpRow))).Value = 0
      End If
            
      intLookUpRow = intLookUpRow + 1
   
   Loop

End Sub
 
Sorry Skipvought I did not see the variant part . . . Changed as seen below and still get same error.

Thanks again!
pbt1234

Code:
Private Sub Newday_click()
Dim strLookUpValue As String
Dim strAnswer As String
Dim rngLookUpRange As Range
Dim intLookUpRow As Integer
Dim myvar As Variant

   Do While intLookUpRow < 138
      Set rngLookUpRange = Worksheets("Inventory").Range("A2:D1000")
      strLookUpValue = Worksheets("OPSC").Range("B" & (4 + (7 * intLookUpRow))).Value
           
      myvar = Application.WorksheetFunction.VLookup(strLookUpValue, rngLookUpRange, 4, False)
        
      If Not IsError(myvar) Then
         Sheets("OPSC").Range("E" & (4 + (7 * intLookUpRow))).Value = _
            (Application.WorksheetFunction.VLookup(strLookUpValue, rngLookUpRange, 4, False)) / 1000
      Else
         Sheets("OPSC").Range("E" & (4 + (7 * intLookUpRow))).Value = 0
      End If
            
      intLookUpRow = intLookUpRow + 1
   
   Loop

End Sub
 
rather than testing the result, try testing the function
Code:
if (iserror(Application.WorksheetFunction.VLookup(strLookUpValue, rngLookUpRange, 4, False)) then
'there is an error
Else
'no error
End If

Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
Tried that this morning, still same message . . . .

Thanks again - will keep trying.

pbt1234

Code:
Private Sub Newday_click()
Dim strLookUpValue As String
Dim strAnswer As String
Dim rngLookUpRange As Range
Dim intLookUpRow As Integer

   Do While intLookUpRow < 138
      Set rngLookUpRange = Worksheets("Inventory").Range("A2:D1000")
      strLookUpValue = Worksheets("OPSC").Range("B" & (4 + (7 * intLookUpRow))).Value
           
      If (IsError(Application.WorksheetFunction.VLookup(strLookUpValue, rngLookUpRange, 4, False))) Then
         Sheets("OPSC").Range("E" & (4 + (7 * intLookUpRow))).Value = 0
      Else
         Sheets("OPSC").Range("E" & (4 + (7 * intLookUpRow))).Value = _
            (Application.WorksheetFunction.VLookup(strLookUpValue, rngLookUpRange, 4, False)) / 1000
      End If
            
      intLookUpRow = intLookUpRow + 1
   
   Loop

End Sub
 

as I previously inplied loose WorksheetFunction...

Code:
      myvar = Application.VLookup(strLookUpValue, rngLookUpRange, 4, False)

Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
Ok then - some slightly more sophisticated error trapping then
Code:
on error resume next [COLOR=green]'stop errors interrupting code / turns off auto error handling[/color]
myVar = WorksheetFunction.VLookup(strLookUpValue, rngLookUpRange, 4, False)[COLOR=green]'set vlookup[/color]
errnum = Err [COLOR=green]'obtain error number generated[/color]    
    On Error GoTo 0 [COLOR=green]'reset error handling to be back on again [/color]
        Select Case errnum
          Case 0 [COLOR=green]'no error[/color]
           Sheets("OPSC").Range("E" & (4 + (7 * intLookUpRow))).Value = myVar / 1000
          Case else [COLOR=green]' error generated[/color]
           Sheets("OPSC").Range("E" & (4 + (7 * intLookUpRow))).Value = 0
        End Select


Rgds, Geoff

"Three things are certain: Death, taxes and lost data. Guess which has occurred"

Please read FAQ222-2244 before you ask a question
 
SkipVought - -

Did not see that you had dropped the WorksheetFunction porition. Once I did that worked like a champ!!!! Thanks so much to all of you for helping with this problem. I have NEVER been here with a question that someone has not been able to help with.

Thanks again!
pbt1234
 
pbt,

Use one or the other -- not both

Application

WorksheetFunction

Skip,
[sub]
[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue][/sub]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top