This above image is from an excel file row data. I want to read each cell from 1 to N cells as array in VBA
dim array() as Variant
I want output as
array(0)= 1
array(1)= West
array(2)= Product 9
array(3)= 15
.
.
.
array(N)= N column
This above image is from an excel file row data. I want to read each cell from 1 to N cells as array in VBA
dim array() as Variant
I want output as
array(0)= 1
array(1)= West
array(2)= Product 9
array(3)= 15
.
.
.
array(N)= N column
Assuming you want to extract the row 1, here is the code:
Dim array1() As Variant
Dim LastColumn As Long
LastColumn = ActiveSheet.Cells(1, 1).End(xlToRight).Column
ReDim array1(LastColumn - 1)
For LoopVar = 1 To LastColumn
array1(LoopVar - 1) = Cells(1, LoopVar).Value
Next
To check if your array has the required values, use this code:
For Each cellvalue In array1
MsgBox (cellvalue)
Next
You can double transpose your values into a 1d-array:
Sub Test()
Dim arr As Variant
Dim x As Long
With ThisWorkbook.Worksheets("Sheet1")
x = .Cells(1, .Columns.Count).End(xlToLeft).Column
arr = Application.Transpose(Application.Transpose(.Range(.Cells(1, 1), .Cells(1, x)).Value))
End With
End Sub
Finally I fixed the issues. Thank you @Shyam i just modified your code little. also I thank all other members who are all helped me.
Sub excelRowRangeToArray()
Dim rowNo As Long
Dim arraycell() As Variant
Dim LastColumn As Long
rowNo = 1 'row number which you want make as one dimensional array
LastColumn = ActiveSheet.Cells(rowNo, Columns.Count).End(xlToLeft).Column
ReDim arraycell(LastColumn - 1)
For i = 1 To LastColumn
arraycell(i - 1) = Cells(rowNo, i).Value
Next
'To check if your array has the required values, use this code:
For Each cellvalue In arraycell
MsgBox (cellvalue)
Next
End Sub