#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 90
Default Error 2042

Hi
I'm trying to compare a variable to a value in a range of cells. Depending
on the variable, I need it to be entered into one of two worksheets. It
works fine if the variable is within the range - the variable is entered onto
Sheet1. If the variable is not within the range, then I get the error 2042
because it can't find the value - and the variable should be entered onto
Sheet2.

I do have a second range of cells so that if the variable is not in the
first range, it will be in the second range. How do I get this working
correctly?

Dim ShopName as String

Set Rng = Worksheets("IllinoisShopNames").Range("A2:A50")
Results = Application.VLookup(Range("E5").Value, Rng, 1, False)
Do While ShopName < ""
If Results = ShopName Then
do stuff on Sheet1
Else
do other stuff on Sheet2
End If
Loop

TIA
Fred
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Error 2042

You could use:

Dim ShopName as String
dim Results as variant

Set Rng = Worksheets("IllinoisShopNames").Range("A2:A50")

'it's nice to qualify the ranges!
Results = Application.VLookup(worksheets("somesheetname").Ra nge("E5").Value, _
Rng, 1, False)

if iserror(results) then
'do stuff on sheet2
else
'do stuff on sheet1
end if

======
I don't quite get the loop portion, though.

And since you're only checking to see if there's a match, then =match() seems
like a more appropriate function to use:

Dim ShopName as String
dim Results as variant

Set Rng = Worksheets("IllinoisShopNames").Range("A2:A50")

Results = Application.match(worksheets("somesheetname").Rang e("E5").Value,rng,0)

if iserror(results) then
'do stuff on sheet2
else
'do stuff on sheet1
end if

Fred wrote:

Hi
I'm trying to compare a variable to a value in a range of cells. Depending
on the variable, I need it to be entered into one of two worksheets. It
works fine if the variable is within the range - the variable is entered onto
Sheet1. If the variable is not within the range, then I get the error 2042
because it can't find the value - and the variable should be entered onto
Sheet2.

I do have a second range of cells so that if the variable is not in the
first range, it will be in the second range. How do I get this working
correctly?

Dim ShopName as String

Set Rng = Worksheets("IllinoisShopNames").Range("A2:A50")
Results = Application.VLookup(Range("E5").Value, Rng, 1, False)
Do While ShopName < ""
If Results = ShopName Then
do stuff on Sheet1
Else
do other stuff on Sheet2
End If
Loop

TIA
Fred


--

Dave Peterson
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
VBA code to erase an array element returning Error 2042 ??? Patachoup Excel Programming 4 June 5th 07 11:31 AM
vlookup error 2042 jer Excel Programming 0 November 27th 06 02:31 PM
Error Handling - On Error GoTo doesn't trap error successfully David Excel Programming 9 February 16th 06 05:59 PM
Excel xlodbc error 2042 with SQLRetrieve ALK[_2_] Excel Programming 0 July 28th 05 03:05 PM
Error 2042 Barbara[_3_] Excel Programming 1 September 8th 03 08:16 PM


All times are GMT +1. The time now is 10:04 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"