I am new to VBA and I need to expand an existing Worksheet and keep its formatting. There are 7 sections with variable length (in rows) and a width of 14 columns that need to be completed. So what I am trying to do is the following:
- find the lines where the sections start
- select the data out of each section and save it into an array (i thought about this as an array of length 7 and each entry contains a 2-dim. array with the data in it)
- select my new data and expand the existing array (created in the last step) with that new data
- override the current sheet with my new created array
- add formatting
I managed to do step 1 and am currently struggling at step 2: I need to create an array with variable length where I can insert the data.
My code so far:
' this should create the array with the 7 entries ' "myArray" contains the row-numbers where the sections start Function GenerateSheetArray(sheet As Worksheet, myArray As Variant) As Variant Dim finalArray As Variant Dim myInt As Integer 'here each entry should be filled For i = 0 To 6 myInt = myArray(i) finalArray(i) = GenerateArrayPart(sheet, myInt) Next GenerateSheetArray = finalArray End Function 'This should fill each entry with the data of corresponding section Function GenerateArrayPart(sheet As Worksheet, headline As Integer) As Variant Dim leftIndex As Integer, rightIndex As Integer, rowcount As Integer Dim sheetArray() As Variant rowcount = 0 leftIndex = 1 rightIndex = 14 i = headline + 1 Do While sheet.Cells(i, 1) <> "" rowcount = rowcount + 1 i = i + 1 Loop If (rowcount > 0) Then For colIndex = leftIndex To rightIndex For rowIndex = 1 To rowcount Row = headline + rowIndex sheetArray(rowIndex - 1, colIndex - 1) = sheet.Cells(Row, colIndex) Next Next End If GenerateArrayPart = sheetArray End Function
Now my problem is, that VBA throws an error at this line:
'atm rowIndex and colIndex are 1, Row is 40 'I know that there is data in that cell sheetArray(rowIndex - 1, colIndex - 1) = sheet.Cells(Row, colIndex)
Index out of range
How can this happen? I thought that
variant can be pretty much everything and also does not need bounds to be used?
You are not having any value in the array. Thus, the array is only declared and not dimensionized.
Dim finalArray As Variant Redim finalArray(6)
Now, the array would have 7 values inside. From 0 to 6. The same error happens in the
Function GenerateArrayPart, with the array
sheetArray. There you need to declare the array as a multidimensional array. E.g.
Redim sheetArray (N, M).
To see some small working sample, take a look at the code below:
Sub TestMe() Dim finalArr As Variant ReDim finalArr(6) Dim i As Long For i = LBound(finalArr) To UBound(finalArr) finalArr = GenerateArrPart(i) Next i For i = LBound(finalArr) To UBound(finalArr) Debug.Print i; finalArr(i, i) Next i End Sub Public Function GenerateArrPart(a As Long) As Variant Dim i As Long Dim arrReturn As Variant ReDim arrReturn(a + 1, a + 1) For i = LBound(arrReturn) To UBound(arrReturn) arrReturn(i, i) = a * i Next i GenerateArrPart = arrReturn End Function
This is the output:
0 0 1 6 2 12 3 18 4 24 5 30 6 36 7 42