Assigning named range to a 2-dimensional array

G

Guest

I have a named range ("AdjHoursLookup") that currently occupies 12 cells (3
rows x 4 columns), but the number of cells will vary over time.

Is it possible to assign the named range to a 2-dimensional array? When I
excute the following two lines of code, for example, I get an "object
required" error message.

Dim AdjHoursArray As Range
Set AdjHoursArray = Range("AdjHoursLookup").Value

Any help would be greatly appreciated. Thanks.
 
G

Guest

I just discovered that by removing ".Value", it fixes the "object required"
error message problem.

But now I have another issue. Since the size of the named range will vary
over time, is there a way I can determine the number of non-blank elements in
the array?

Thanks again for any help.
 
D

Dave Peterson

Dim AdjHoursArray As Range
Set AdjHoursArray = Range("AdjHoursLookup")
msgbox application.counta(adjhoursarray) & vblf & adjhoursarray.cells.count

=counta() will count all the formulas and constants that are in that
range--including formulas that evaluate to "" (like =if(a1=5,"ok",""))
 
G

Guest

Note that te title of this thread seems at odds with the code quoted;
Dim AdjHoursArray As Range
declares a Range object, which is not an array.

To assign the named range to an array do this:

Dim AdjHoursArray As Variant 'optional, but required if Option Explicit on.
AdjHoursArray = Range("AdjHoursLookup")


AdjHoursArray is now an array of variants (1 to 3, 1 to 4)
 
G

Guest

Dave,
Thanks for your help!
Bob


Dave Peterson said:
Dim AdjHoursArray As Range
Set AdjHoursArray = Range("AdjHoursLookup")
msgbox application.counta(adjhoursarray) & vblf & adjhoursarray.cells.count

=counta() will count all the formulas and constants that are in that
range--including formulas that evaluate to "" (like =if(a1=5,"ok",""))
 
G

Guest

Thanks!


p45cal said:
Note that te title of this thread seems at odds with the code quoted;
Dim AdjHoursArray As Range
declares a Range object, which is not an array.

To assign the named range to an array do this:

Dim AdjHoursArray As Variant 'optional, but required if Option Explicit on.
AdjHoursArray = Range("AdjHoursLookup")


AdjHoursArray is now an array of variants (1 to 3, 1 to 4)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top