Vlookup in macro won't work

merry_fay

Board Regular
Joined
Oct 28, 2010
Messages
54
Hiya,

I know there's tons of examples of this online, but I've looked & can't for the life of me work out why my vlookup won't work. Here's my code:

Dim SC As Integer
Dim SN As String

SC = Range("S_Code").Value
SN = Application.WorksheetFunction.VLookup(SC, Range("S_Code_R"), 2, False)


SN is to be used to set the value of cell "S_Name".
I've tried it using Range("S_Code").Value, Range("S_Code"), "S_Code", S_Code in place of the SC but it keeps coming up with the error:

Method 'Range' of object '_Worksheet' failed

SC does have a value & is definitely included in the S_Code_R range

Any suggestions?

Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I think the issue is in this line:

Code:
SC = Range("S_Code").Value

Unless I am missing something, that is not a valid address to get a value from... If S_Code is a Named Range on a Worksheet, then it should be:

Code:
SC = Sheet1.Range("S_Code").Value

If its not a named range, then what is it referring to?
 
Upvote 0
Yes, "S_Code" is a named range.

I tried adding the sheet name in & also as you typed it, but it makes that line fall over instead of the next one.

If I just run the code:

SC = Range("S_Code").Value
MsgBox SC

I get a messagebox with the value I'm expecting S_Code to be in it, so from that I assume that line of code is working fine.

What I want to do is look up the value from named range (single cell) S_Code in the named range S_Code_R & bring back a result.

Thanks
 
Upvote 0
I've just had a bit more of a play & with this:

Dim SC As Integer
Dim SN As String
Dim SCR As Range

SC = Range("S_Code").Value
SCR = Range("'[Spreadsheet Name.xls]Lookups'!$A1:$D5000")
SN = Application.WorksheetFunction.VLookup(SC, SCR, 2, False)

It's the SCR= line which is coming up with the same error message, so it seems I'm having problems getting the macro to look at the range I want.

Any sugestions? I'm completely perplexed.

Thanks!
 
Upvote 0
This probably doesn't help but the code in your first post worked fine here.

If it wasn't an insulting suggestion I would ask you to treble-check your named ranges.
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,521
Members
452,923
Latest member
JackiG

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top