Here is the code, in here i am assigning the range("A1:AH1") to array arr1 but when i try to read the array by giving the statement debug.print the first entry in the array is itself coming as empty
The code:
Private Sub CommandButton5_Click()
Dim arr1() As Variant
Dim i As Integer
Dim j As Integer
Dim wkbnew As Workbook
Dim wks As Worksheet
Dim R As Long
Dim C As Long
Set wks = Worksheets.Add(after:=Sheets(wkb1.Sheets.Count))
wks.Name = "MySheet"
wkb1.Sheets("Heading2").Activate
i = 1
j = 1
arr1 = Range("A1:AH1")
Debug.Print Range("A1")
For R = 1 To UBound(arr1, 1) ' First array dimension is rows.
For C = 1 To UBound(arr1, 2) ' Second array dimension is columns.
i = 1
Debug.Print arr1(R, C)
Debug.Print Sheets("ODS_DATA").Cells(1, i)
Do Until IsEmpty(Sheets("ODS_DATA").Cells(1, i))
Debug.Print Sheets("ODS_DATA").Cells(1, i)
If arr1(R, C) = Sheets("ODS_DATA").Cells(1, i) Then
If Sheets("MySheet").Range("A1") = "" Then
nextcol = 1
Else
nextcol = Sheets("MySheet").Cells(1, Columns.Count).End(xlToLeft).Column + 1
End If
Sheets("ODS_DATA").Cells(1, i).EntireColumn.Copy Sheets("MySheet").Cells(1, nextcol)
Exit Do
End If
i = i + 1
Loop
Next C
Next R
End Sub
The code:
Private Sub CommandButton5_Click()
Dim arr1() As Variant
Dim i As Integer
Dim j As Integer
Dim wkbnew As Workbook
Dim wks As Worksheet
Dim R As Long
Dim C As Long
Set wks = Worksheets.Add(after:=Sheets(wkb1.Sheets.Count))
wks.Name = "MySheet"
wkb1.Sheets("Heading2").Activate
i = 1
j = 1
arr1 = Range("A1:AH1")
Debug.Print Range("A1")
For R = 1 To UBound(arr1, 1) ' First array dimension is rows.
For C = 1 To UBound(arr1, 2) ' Second array dimension is columns.
i = 1
Debug.Print arr1(R, C)
Debug.Print Sheets("ODS_DATA").Cells(1, i)
Do Until IsEmpty(Sheets("ODS_DATA").Cells(1, i))
Debug.Print Sheets("ODS_DATA").Cells(1, i)
If arr1(R, C) = Sheets("ODS_DATA").Cells(1, i) Then
If Sheets("MySheet").Range("A1") = "" Then
nextcol = 1
Else
nextcol = Sheets("MySheet").Cells(1, Columns.Count).End(xlToLeft).Column + 1
End If
Sheets("ODS_DATA").Cells(1, i).EntireColumn.Copy Sheets("MySheet").Cells(1, nextcol)
Exit Do
End If
i = i + 1
Loop
Next C
Next R
End Sub