Today I started studying Arrays in VBA.
After trying a few easy scripts I wanted to create one that is useful for my project.
In my excelsheet I have a datatable that needs to be transformed to new worksheets. Only for each column that has "Detail" in row 4.
The easiest way to imagine this would be by writing the values per relevant column to an array, reading and writing the results to a new sheet, and performing the action again.
But I think I'm using a wrong method to write the variables to my array. I looked through my code and all my declarions are not correct.
Could you help me out, how I can change the writing to the array correct?
Sub Import_data()
Dim LastCol As Integer
Dim LastRow As Long
Dim WS As Worksheet
Dim Arr() As Variant
Dim dim1 As Long, dim2 As Long
Set WS = Sheets("Budget to Table")
' Copy data from Budget to Table
With WS
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
dim1 = .Cells(.Rows.Count, "B").End(xlUp).Row - 5
dim2 = 4
' Copy information
For i = 3 To LastCol
If Cells(4, i).Value = "Detail" Then
ReDim Arr(0 To dim1, 0 To dim2)
For dim1 = LBound(Arr, 1) To UBound(Arr, 1)
For dim2 = LBound(Arr, 2) To UBound(Arr, 2)
Arr(dim1, 0) = Range(Cells(dim1, 2)) 'Should have the variable length but always column B
Arr(dim1, 1) = Range(Cells(dim1, i)) 'Should have the variable length but always column i
Arr(dim1, 2) = Range(Cells(1, i)) 'Is always the same header info from row 1 of the chosen column
Arr(dim1, 3) = Range(Cells(2, i)) 'Is always the same header info from row 2 of the chosen column
Arr(dim1, 4) = Range(Cells(3, i)) 'Is always the same header info from row 3 of the chosen column
Next dim2
Next dim1
End If
'writing the contents in a new sheet
Worksheet.Add
For dim1 = LBound(Arr, 1) To UBound(Arr, 1)
For dim2 = LBound(Arr, 2) To UBound(Arr, 2)
ActiveCell.Offset(dim1, dim2).Value = Arr(dim1, dim2)
Next dim2
Next dim1
Erase Arr
Next i
End With
End Sub
If I need to provide any more guidance please let me know.
I guess that the value of the dim1 and dim2 are never changing, so this doesn't create the loop i'm after.
edit: I uploaded the file here: https://dubblej15.stackstorage.com/s/C0DrKzFDxn4gY4U
I manually performed the action twice, what my result should look like. Maybe there is a better or easier way, but I thought that arrays could fit the job perfectly.
Thanks in advance!
Dim arr as Variant: arr = myRange--> a 2D array. See Chip Pearsons web page on [Arrays and Ranges in VBA](VBA Arrays And Worksheet Ranges). And posting an annotated screenshot to show exactly what you are trying to do would be helpful.Redim arr(1 to .Areas.Count): For each W in .Areas: I = I+1: arr(I) = W.Value2`WS, but later on start using the default properties on the omnipresent Application object. What I mean is in this statement:LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column,.Cellsis fromWS. However in this statement:If Cells(4, i).Value = "Detail" Then,Cellsis from the Application object. Your use ofRangeis similarly suspect. What is Worksheet inWorksheet.Add? Normally, a Worksheet does not have an Add method.vals = Range("A2").Resize(100,5).Valuetype of syntax, and to write one reverse it withRange("A2").Resize(100,5).Value=vals. Declare the array withDim vals() as Variantand make sure the sizes are likeReDim vals(1 to 100, 1 to 5)with1based indexing.