To increase the performance of my VBA code for large spreadsheets, I'm converting the sheets into 2D Arrays of Strings
that the program can refer to instead of leaving open and looping through the memory intensive spreadsheets. The sheets dramatically slow down my computer, and at present the macro is slower than doing it by hand (I've removed many unnecessary columns and removed all formatting from these sheets, and no formulas seem to extend past the used range-- they're also stored as .xlsb's. They're already about one-half to two-thirds the size of the originals, so I don't think there's anything else to be done to optimize them).
Note that I'm developing in outlook, but relying heavily on data from excel sheets- the use case is an email auto-responder that searches the sheets for an ID supplied in an email, and then replies to that email with a phone number from the sheets. I have the proper references in place, and the program opens the sheets fine, just (painfully) slowly.
I'd like to use nested For
loops to load the spreadsheets into the arrays programmatically, and then store those arrays in another array so they can in turn be looped through. In my research, I've found code to make jagged arrays in VBA (How do I set up a "jagged array" in VBA?), and 2D Arrays in VBA (Multi-dimensional array in VBA for Microsoft Word on Mac) but not arrays of 2D Arrays.
This is the code I wrote to make the 2D arrays- the Dim
and ReDim
lines throw syntax errors.
For k = LBound(sheetsArr) To UBound(sheetsArr)
Dim myWbksArr(k)() As String
ReDim myWbksArr(k)(sheetsArr(k).UsedRange.Rows.Count, sheetsArr(k).UsedRange.Columns.Count)
Next k
Where sheetsArr
is an array of Worksheets
into which I copied the sheets I'm referencing to avoid another for
loop to iterate through Workbooks as well, using
Dim sheetsArr() As Worksheet, runningIndex As Long
runningIndex = 0
ReDim sheetsArr(1 To totalSheets) 'It would make sense to me to extend this syntax to an array as above, since an array is itself a type/object in other languages
For j = LBound(myWbks) To UBound(myWbks) 'j iterates through the workbooks
Set myWbks(j) = Workbooks.Open(FileName:=arr(j), UpdateLinks:=False) 'false should suppress update links msgbox
For Each sh In myWbks(j).Worksheets 'iterates through the worksheets in each workbook
sheetsArr(runningIndex) = sh 'add the worksheet to an array of worksheets so we don't have to use another for loop to get through the workbook layer too
runningIndex = runningIndex + 1
Next
Next j
What's the correct syntax to create an array of 2D arrays using for loops?