Named array of variable size

pingme89

Board Regular
Joined
Jan 23, 2014
Messages
176
Office Version
  1. 365
Platform
  1. Windows
I want to name an array to use in a formula with Index and Match.

Specifically speaking, I have 2 Hidden sheets. Every time the spreadsheet opens, the user is prompted to select 2 files. The contents of each file will be copied onto each of the Hidden Sheets. The problem is that I don't know how to declare the array with the array size changing depending on the user selected file.

I tried this but I get an error saying that the values must be constants.

Dim HRange(1 To HRow, 1 To HCol) As String
Dim DRange(1 To DRow, 1 To DCol) As String
Dim HUnitRange(1 To HRow, HUnitCol To HUnitCol) As String
Dim HVINRange(1 To HRow, HVINCol To HVINCol) As String
Dim DUnitRange(1 To DRow, DUnitCol To DUnitCol) As String
Dim DVINRange(1 To DRow, DVINCol To DVINCol) As String

HRow, HCol, DRow, DCol are integers and the values are obtained by counting the number of rows of each user selected file.

On sheet1 I want to use as Index and Match combination referring to the User Selected files. Can someone explain to me how this can be done?

Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try:
Code:
...
Dim HRange() As String
ReDim HRange(1 To HRow, 1 To HCol)
...


Thanks. The array declaration problem went away, but now I have a problem setting the array.

HLastAddress = Worksheets("HData").Cells(HRow, HCol).AddressDLastAddress = Worksheets("DData").Cells(DRow, DCol).Address


Set HRange = ActiveWorkbook.Worksheets("HData").Range("A1:" & HLastAddress)
Set DRange = ActiveWorkbook.Worksheets("DData").Range("A1:" & DLastAddress)

I can't see what is wrong.
 
Upvote 0
Try this (no need to ReDim ... As ...):
Code:
...
Dim HRange()
HRange = ActiveWorkbook.Worksheets("HData").Range("A1:" & HLastAddress)
...
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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