Saving data via a dynamic range to a VBA Array

DonEB

Board Regular
Joined
Apr 26, 2016
Messages
133
Office Version
  1. 2019
Platform
  1. Windows
I've attempted to find a solution on the internet but have failed in coming up with a simple way to pose the question. So here is my problem with a little more detail. I have a range of data that can be found in Sheets("CommonData2").Range("E6:AH6"). However, the actual range of data can be anywhere from in just 5 columns to as many as in all 30 columns.

To determine the actual range size, I use the following: ColP = Sheets("CommonData2").Cells(6, 34).End(xlToLeft).Column

I resize my array using the newly found information which tells me which column the row ends.

Now, here's the problem I'm trying to solve. I'm having difficulty in coming up with a solution that would allow me to populate the array with the information contained within the range of E6:AH6. As previously stated, this range could be anywhere from E6:I6 to E6:AH6.

ColP = Sheets("CommonData2").Cells(6, 34).End(xlToLeft).Column
ReDim arr_CommonD2(1 To ColP - 4)

Normally, I would simply save a known range to the array, as follows:
arr_CommonD2 = Sheets("CommonData2").Range("E6:I6") 'In this example, data only exists in the 5 cells indicated. However, it could range from 5 to 30 cells.

But, when the range is dynamic and the size is not known until I perform the last column check above, how do I then save the data in the range to the array?

My best guess is to initiate a loop but my searching has not come up with a solution and I'm at a loss as to where to go from here.
For i = 5 To ColP
arr_CommonD2 =
Next i

Thanks to anyone who might be able to provide a little guidance.

Don
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
First, declare arr_CommonD2 as variant, not fixed array
Second, to use Range(Cell1, Cell2) to get a range from Cell 1 to Cell2
also using columns.Count to get the farest column index (XFZ), not harcoded "34"
Like this
PHP:
Dim ColP&, arr_CommonD2 As Variant
With Sheets("CommonData2")
    ColP = .Cells(6, Columns.Count).End(xlToLeft).Column
    arr_CommonD2 = .Range("E6", Cells(6, ColP)).Value
End With
 
Upvote 1
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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