2 Dimensional Array from Non Sequential Columns

mabbutt

Board Regular
Joined
Oct 4, 2014
Messages
106
Hi,

I would like to create a 2-dimensional array from two columns that are not next to each other.

This works for when they are next to each other e.g. A,B F,G etc but I would like to have columns D,P for example:

VBA Code:
    lr = .Cells(.Rows.Count, 6).End(xlUp).Row
    arr = .Range("F1:G" & lr)

I have looked at disjoint arrays and several other examples but cannot get the code to work at all.

I have also tried to build the range using this variation:

Code:
    lr = .Cells(.Rows.Count, 6).End(xlUp).Row
    arr = .Range("F1", "H" & lr)

Again I can get this method to work but only if the columns are side by side.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Give this a try (no need for the lr variable)...
VBA Code:
Arr = Application.Index(Cells, Evaluate("ROW(1:" & Cells.Find("*", , xlFormulas, , xlRows, xlPrevious).Row & ")"), Array(4,16))
If you want to preserve the lr variable, then...
Code:
lr = .Cells(.Rows.Count, 6).End(xlUp).Row
Arr = Application.Index(Cells, Evaluate("ROW(1:" & lr & ")"), Array(4,16))
 
Upvote 0
Solution
Give this a try (no need for the lr variable)...
VBA Code:
Arr = Application.Index(Cells, Evaluate("ROW(1:" & Cells.Find("*", , xlFormulas, , xlRows, xlPrevious).Row & ")"), Array(4,16))
If you want to preserve the lr variable, then...
Code:
lr = .Cells(.Rows.Count, 6).End(xlUp).Row
Arr = Application.Index(Cells, Evaluate("ROW(1:" & lr & ")"), Array(4,16))
Hi Rick,

I'm extremly sorry for the late reply as I have been unwell.

This is absolutely perfect and so simple.

Thank you!
 
Upvote 0
If you want to preserve the lr variable, then...
VBA Code:
lr = .Cells(.Rows.Count, 6).End(xlUp).Row
Arr = Application.Index(Cells, Evaluate("ROW(1:" & lr & ")"), Array(4,16))
But Do you need to is it not at the end of the day lr=Ubound(Arr)??
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

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