Declare a range as a constant rather than a variable?

bluto32

New Member
Joined
Jan 5, 2011
Messages
37
I have a feeling I am going to kick myself once this is pointed out to me...

I have written some lengthy code which does what I want, but I would like to "polish" it up, and get into good habits with using objects properly.

In the code below, I am happy that "a" is a variable which has been set to 10 for now, but can be changed later. "b" is a constant, and cannot be changed from its initial value of 25.

Code:
Dim a As Integer
a = 10
 
Const b as Integer = 25

But... why does it not work with ranges?

Code:
Dim c As Range
Set c = Range("A1:B4")
 
Const d As [COLOR=red]Range[/COLOR] = Range("A1:B4")

I get the error "Expected: type name", and it's the red bit which seems to be causing the problem.

Bluto
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Not sure how using a constant instead of a variable here would be good practice.

Anyway you can't declare a Range as a constant because it isn't constant.
 
Upvote 0
Constants can only be data types, not Objects.
 
Upvote 0
You could make it a Public variable.

Code:
Dim abRange as Range

Sub InitialSetup
  Set abRange = Worksheets(1).Range("A1:B4")
End Sub

In another routine such as an Open Event or another sub:
Code:
Private Sub Workbook_Open()
  InitialSetup
End Sub
 
Upvote 0
Thank you.

I'm just going to stick with the standard "Dim" and "Set" lines, given that objects are not allowed to be declared as constants.

Bluto
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

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