VBA - Load Alternating column Array from values

sbawnh

Board Regular
Joined
Feb 25, 2019
Messages
50
Office Version
  1. 365
Platform
  1. Windows
Hello,
I want to load an array with values of my column headers like below.
vHeaders = .Range("A1:A10").Value

However, I only want the alternating columns added and for it to be dynamic based on columns from the endcol
Essentially without hardcode addresses
VHeadersA = .Range("A1","A3","A5","A7","A9").Value
VHeadersA = .Range("A2","A4","A6","A8","A10").Value

I've thought to hide columns, then add the values of the visible headers, but It only loads the first value to a variable
 

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)
Your request is ambiguous because you state you want alternating columns, but then give a range with alternating rows , which do you want??
 
Upvote 0
If you really want two arrays with alternate columns this code will do it for you
It will write the results to sheets 2 and 3:
VBA Code:
Sub test()
Dim colA()
Dim Colb()
inarr = Range("A1:L20")
Ncols = Int((UBound(inarr, 2) + 1) / 2)
Nrows = UBound(inarr, 1)
ReDim colA(1 To Nrows, 1 To Ncols)
ReDim Colb(1 To Nrows, 1 To Ncols)
For i = 1 To Ncols
 
  For j = 1 To Nrows
   colA(j, i) = inarr(j, 1 + (i - 1) * 2)
  Next j
Next i
For i = 1 To Ncols
  If i * 2 > UBound(inarr, 2) Then Exit For
  For j = 1 To Nrows
   Colb(j, i) = inarr(j, i * 2)
  Next j
Next i

With Worksheets("Sheet2")
 .Range(.Cells(1, 1), .Cells(Nrows, Ncols)) = colA
End With
With Worksheets("Sheet3")
 .Range(.Cells(1, 1), .Cells(Nrows, Ncols)) = Colb
End With
End Sub
 
Upvote 0
Solution
Offthelip! Thank you so much for this code. I really appreciate it.

Sorry about the typos where I changed the row's not the columns. You've got it right
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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