VBA ranges - I want to add a RANGE -- Perhaps like DEFINE NAMES?


Posted by bill on June 01, 2001 11:43 AM

Hi,

I want to assign a value to a range that I can later access anywhere.

Just like being able to DEFINE NAMES, i'd like to do that in code...

Thnx!



Posted by Russell on June 01, 2001 1:41 PM

Well, you can just define a variable as being of type Range. Then you can access that range all you want via the variable name. You can also give a name to the range, but that is more for if you would want to use it in the Excel spreadsheet itself later. Example follows.

Sub RangeTest()

Dim rng As Range

Set rng = Range("A1:C3")

' Now you can refer to the Range by referring to rng.
rng.Name = "rngTest"
' You can now refer to the range either by using rng - OR, something like this:

Range("rngTest").Cells(1, 1) = 2

End Sub

Hope this helps,

Russell