VBA Loop on array's columns

Опубликовано: 18 Июнь 2026
на канале: Simplified Excellence
628
7

Loop on all columns of an array.

Code:
Sub loop_on_columns_Array()

Dim ws_sheet As Worksheet
Dim lastrow As Long
Dim lastcolumn As Long
Dim array_data() As Variant

'identify worksheet
Set ws_sheet = Worksheets(2)

'find table's limits
lastrow = ws_sheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ws_sheet.Cells(1, Columns.Count).End(xlToLeft).Column

'save table in array
array_data = ws_sheet.Range(ws_sheet.Cells(1, 1), ws_sheet.Cells(lastrow, lastcolumn))

'loop on columns
For j = LBound(array_data, 2) To UBound(array_data, 2)
'your code here
'example: read columns header
MsgBox ("Header of the column " & j & ": " & array_data(1, j))
Next

End Sub

-------------------------------
NEED HELP?
-------------------------------
Leave a comment with your questions, issues or projetcs and I will try to answer with a video.

-------------------------------
THANK YOU
-------------------------------
For watching my videos,
Your shares
And your comments!