Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Vlookup in VBA

I am trying to do a Vlookup. My lookup value is on the MainData sheet and my
lookup range is on the DeptLookup sheet. If there is a match, the code works
fine. However, if there is no match, I get a Runtime error '1004' -
Application defined or object defined error on the line

y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1, False

My code is:
Set wb = Workbooks("TestWorkbook.xls")
Set ws = wb.Sheets("DeptLookup")
Set WS2 = wb.Sheets("MainData")
Set rng = ws.Range("$A$3:$C$59")
Dim y As Variant

y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,
False)
If Not IsError(y) Then
ActiveCell.Offset(0, -34).Value =
Application.WorksheetFunction.VLookup_(ActiveCell. Offset(0, -31).Value, rng,
1, False)

Would anyone be able to tell me what I am doing wrong?

Thanks in advance!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Vlookup in VBA

If you qualify Vlookup with WorksheetFunction, then failure to match will
results in a 1004 error (this is by design).

If you qualify Vlookup with Application instead, you can use the IsError
test.

Set wb = Workbooks("TestWorkbook.xls")
Set ws = wb.Sheets("DeptLookup")
Set WS2 = wb.Sheets("MainData")
Set rng = ws.Range("$A$3:$C$59")
Dim y As Variant

y = Application.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1, False)
If Not IsError(y) Then
ActiveCell.Offset(0, -34).Value = y
End if


--
Regards,
Tom Ogilvy



"sharonm" wrote in message
...
I am trying to do a Vlookup. My lookup value is on the MainData sheet and

my
lookup range is on the DeptLookup sheet. If there is a match, the code

works
fine. However, if there is no match, I get a Runtime error '1004' -
Application defined or object defined error on the line

y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,

False

My code is:
Set wb = Workbooks("TestWorkbook.xls")
Set ws = wb.Sheets("DeptLookup")
Set WS2 = wb.Sheets("MainData")
Set rng = ws.Range("$A$3:$C$59")
Dim y As Variant

y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,
False)
If Not IsError(y) Then
ActiveCell.Offset(0, -34).Value =
Application.WorksheetFunction.VLookup_(ActiveCell. Offset(0, -31).Value,

rng,
1, False)

Would anyone be able to tell me what I am doing wrong?

Thanks in advance!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Vlookup in VBA

THANK YOU VERY MUCH!


"Tom Ogilvy" wrote:

If you qualify Vlookup with WorksheetFunction, then failure to match will
results in a 1004 error (this is by design).

If you qualify Vlookup with Application instead, you can use the IsError
test.

Set wb = Workbooks("TestWorkbook.xls")
Set ws = wb.Sheets("DeptLookup")
Set WS2 = wb.Sheets("MainData")
Set rng = ws.Range("$A$3:$C$59")
Dim y As Variant

y = Application.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1, False)
If Not IsError(y) Then
ActiveCell.Offset(0, -34).Value = y
End if


--
Regards,
Tom Ogilvy



"sharonm" wrote in message
...
I am trying to do a Vlookup. My lookup value is on the MainData sheet and

my
lookup range is on the DeptLookup sheet. If there is a match, the code

works
fine. However, if there is no match, I get a Runtime error '1004' -
Application defined or object defined error on the line

y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,

False

My code is:
Set wb = Workbooks("TestWorkbook.xls")
Set ws = wb.Sheets("DeptLookup")
Set WS2 = wb.Sheets("MainData")
Set rng = ws.Range("$A$3:$C$59")
Dim y As Variant

y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,
False)
If Not IsError(y) Then
ActiveCell.Offset(0, -34).Value =
Application.WorksheetFunction.VLookup_(ActiveCell. Offset(0, -31).Value,

rng,
1, False)

Would anyone be able to tell me what I am doing wrong?

Thanks in advance!




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Vlookup in VBA

with function like VLOOKUP and MATCH one can use the on error resume next
followed by whatever on error handle you had. For large blocks of code I
simply find it easier to use a function of my own to "wrap" the excel
function so that this looks after function errors and the error handling in
the caller becomes trivial
---------
result = SafeVLookup("blah", Range("data"), 7)
If result = "" Then
' handle it
Else
' use it
End If
----

Function SafeVLookup(what As String, _
where As Range, _
colChoice As Long) As String
On Error Resume Next
SafeVLookup = WorksheetFunction.VLookup(what, where, colChoice, False)
On Error GoTo 0
End Function


"sharonm" wrote:

THANK YOU VERY MUCH!


"Tom Ogilvy" wrote:

If you qualify Vlookup with WorksheetFunction, then failure to match will
results in a 1004 error (this is by design).

If you qualify Vlookup with Application instead, you can use the IsError
test.

Set wb = Workbooks("TestWorkbook.xls")
Set ws = wb.Sheets("DeptLookup")
Set WS2 = wb.Sheets("MainData")
Set rng = ws.Range("$A$3:$C$59")
Dim y As Variant

y = Application.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1, False)
If Not IsError(y) Then
ActiveCell.Offset(0, -34).Value = y
End if


--
Regards,
Tom Ogilvy



"sharonm" wrote in message
...
I am trying to do a Vlookup. My lookup value is on the MainData sheet and

my
lookup range is on the DeptLookup sheet. If there is a match, the code

works
fine. However, if there is no match, I get a Runtime error '1004' -
Application defined or object defined error on the line

y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,

False

My code is:
Set wb = Workbooks("TestWorkbook.xls")
Set ws = wb.Sheets("DeptLookup")
Set WS2 = wb.Sheets("MainData")
Set rng = ws.Range("$A$3:$C$59")
Dim y As Variant

y = WorksheetFunction.VLookup(ActiveCell.Offset(0, -31).Value, rng, 1,
False)
If Not IsError(y) Then
ActiveCell.Offset(0, -34).Value =
Application.WorksheetFunction.VLookup_(ActiveCell. Offset(0, -31).Value,

rng,
1, False)

Would anyone be able to tell me what I am doing wrong?

Thanks in advance!




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
If (Vlookup 0) working, but what if Vlookup cell does not exist Steve Excel Worksheet Functions 18 November 18th 09 07:33 PM
VLookUp - Does the VLookUp return the exact information? Cpviv Excel Worksheet Functions 2 October 28th 08 09:57 AM
Vlookup in vlookup - taking the result as array name SupperDuck Excel Worksheet Functions 2 June 2nd 07 11:05 AM
Combine VLOOKUP and IF function so #NA isn't returned as a value from VLOOKUP buffgirl71 Excel Discussion (Misc queries) 12 November 14th 06 11:36 PM
Vlookup info being used without vlookup table attached? Excel Worksheet Functions 0 January 25th 05 10:43 AM


All times are GMT +1. The time now is 02:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"