What am I doin wrong with my set range = code?

miless2111s

Active Member
Joined
Feb 10, 2016
Messages
273
Whilst running some test code to work out which is quicker between vlookups and match/offset (see my other thread) I found that I'd caused some sort of odd effect... Within my code I often set up a reference to a named range so that I can use this (for instance in a vlookup) and this has previously worked fine. However I was then finding that my normal method was apparently randomly failing to actually set the range up and so the calculation was failing.

What I normally do:
Code:
Dim MS_Data As Range
Set MS_Data = Range("MS_Data")
Start = Application.WorksheetFunction.VLookup(Vref, MS_Data, 2, False)

This was apparently failing to store or find the MS_Data range and so was causing an error or returning a blank as it didn't know what I was asking it to look in.

However another range, called in the way was working!

I solved this by activating the sheet where the range is located:
Code:
Dim MS_Data As Range
Worksheets("MS Project Bars Import").Activate
Set MS_Data = Range("MS_Data")
Start = Application.WorksheetFunction.VLookup(Vref, MS_Data, 2, False)

as I am such a beginner at this I wonder if I have broken some sort of rule or had I just got lucky all the other times?

Many thanks for any guidance or suggestions as I really can't cope with the idea that the code can appear to behave randomly as I know it can't and it must be user error! :)

Miles
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Sektor

Well-known Member
Joined
May 6, 2011
Messages
2,874
Office Version
  1. 365
Platform
  1. Windows
It seems that your named range has sheet scope rather workbook scope. You can see the scope in Name Manager.
 
Upvote 0

Leith Ross

Well-known Member
Joined
Mar 17, 2008
Messages
1,874
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
Hello miless2111s,

Named Range references can be restricted to either a worksheet or can be can accessed from anywhere (the workbook).
It appears when your named range "MS_Data" was created, it's reference was only to the worksheet "MS Project Bars Import". That is why when you activated the sheet it worked.
 
Upvote 0

miless2111s

Active Member
Joined
Feb 10, 2016
Messages
273
Thanks for your quick replies. Looking at the Name Manager all my ranges are Scope = Workbook and I haven't had this trouble before with these ranges (they're used in a much bigger code set and don't appear to have this issue but now I'm worried that maybe they will hence my concern. I have to admit I didn't even know you could restrict a named range to a sheet! :)
 
Upvote 0

Sektor

Well-known Member
Joined
May 6, 2011
Messages
2,874
Office Version
  1. 365
Platform
  1. Windows
ADVERTISEMENT
You can set sheet scoped named range:
1) in Names Manager
2) in Name Box (to the left of Formula Bar) by prepending sheet name with "!" sign like this: Sheet1!MyRange
 
Upvote 0

miless2111s

Active Member
Joined
Feb 10, 2016
Messages
273
given we've established that the named ranges are in fact for the whole workbook which is what I want what have I don't wrong in my code that means that it wasn't being pulled in?

Many thanks

Miles
 
Upvote 0

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,436
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
ADVERTISEMENT
I have workbooks that have the same named range in each sheet in the same area. Seeing as this is possible I always it best practice to reference sheets().range("Named Range") so it knows exactly which to go to.

on your first macro, without the sheet object, what does the below give you

MsgBox Range("MS_Data").Address
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,681
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
As Barry mentions, you should qualify the range with the sheet it's on - then you don't need to actually activate that sheet first:
Code:
Dim MS_Data As Range
Set MS_Data = Worksheets("MS Project Bars Import").Range("MS_Data")
Start = Application.WorksheetFunction.VLookup(Vref, MS_Data, 2, False)
 
Upvote 0

miless2111s

Active Member
Joined
Feb 10, 2016
Messages
273
I have workbooks that have the same named range in each sheet in the same area. Seeing as this is possible I always it best practice to reference sheets().range("Named Range") so it knows exactly which to go to.

on your first macro, without the sheet object, what does the below give you

MsgBox Range("MS_Data").Address
$AX:$BH.
Interestingly when I select a sheet which I know doesn't have any of the named ranges in and then go to the coding window to run the code it debugs out when asked to do run an offset using the range. With the sheet activating code in play this doesn't happen and the same range address is displayed in the message box
 
Upvote 0

miless2111s

Active Member
Joined
Feb 10, 2016
Messages
273
As Barry mentions, you should qualify the range with the sheet it's on - then you don't need to actually activate that sheet first:
Code:
Dim MS_Data As Range
Set MS_Data = Worksheets("MS Project Bars Import").Range("MS_Data")
Start = Application.WorksheetFunction.VLookup(Vref, MS_Data, 2, False)
Is that always the case or only in some cases? I ask as normally when I see the range function used it doesn't come with the location qualifier (or I'm just missing it when it is!) - as a I say I'm a rank beginner at this so always keen to learn! :)

Thanks

Miles
 
Upvote 0

Forum statistics

Threads
1,195,617
Messages
6,010,729
Members
441,565
Latest member
menangterus556

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
Top