Assigning an Array Using Index/Named Ranges

bookiiemonster

New Member
Joined
Jan 21, 2025
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi!

I have a workbook where I need to refer to specific columns, so I've named them, and then I need to build arrays that need to be dynamic (as in, the amount of values in the columns will change). In all scenarios, each array will just be one column.

VBA Code:
Dim numOfRows As Integer
numOfRows = Application.WorksheetFunction.CountA(Range("MyNamedRange"))
Dim MyNamedRangeArray() As Variant
MyNamedRangeArray = Range("Index(MyNamedRange,1):Index(MyNamedRange," & numOfRows & ")")

The MyNamedRange is Column A in this sheet. Attaching a screenshot of the range and the name set up.

If I do
VBA Code:
Range("Index(MyNamedRange,1):Index(MyNamedRange," & numOfRows & ")").Select
It highlights the correct cells, so I know this Range is written correctly (and I use Range with Index for Named Ranges all the time).

No matter what I do (like add .Value or Value2 to the end of the Range, use Transpose, ect) I can't get MyNamedRangeArray to fill with the values of that Range. I've ran code that prints out the values of an Array or counted the number of values in an Array, and if I blunt force fill the Array, then those codes spit out the values. I really need this to be dynamic because more values will get added or removed from this column (there will always be quite a few, I want to say never less than 10, my example is just using a couple for testing purposes). If there's another way to easily make an Array dynamic in size, happy to rethink my strategy.

Any and all advice is greatly appreciated, as this is my first time using Arrays in VBA (I'm relatively new to VBA coding in general). Hoping I'm just missing something basic.

Thank you!!
 

Attachments

  • Screenshot 2025-01-21 184607.png
    Screenshot 2025-01-21 184607.png
    6.3 KB · Views: 6
  • Screenshot 2025-01-21 184714.png
    Screenshot 2025-01-21 184714.png
    21.1 KB · Views: 6
I don't like the Locals window because there is too much “garbage” in it. In the Watches window I only have what I need.
But these are my personal feelings.

Artik
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,225,759
Messages
6,186,864
Members
453,380
Latest member
ShaeJ73

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