'VBA (Excel) - After seemingly functional For Loop, array of worksheets(i) is nothing
I am trying to loop through all open workbooks, add the book(.Name as of now) to an array if it contains a specific string, and then add the first sheet of that workbook to an array of worksheets that I can later loop through. Even though my loop appears to work in debugger, the array of sheets is nothing and later code breaks with 'variable not set'. I'm a total noob so I'm likely not understanding or missing something (maybe dim/redim/preserve?) Warning this code is pretty messy with other things ive tried and duct-tapey garbage, problem is with ma_SourceSheets. I included everything just in case, and debug at the end.
Dim mwb_Active As Workbook
Dim mws_Active As Worksheet
Dim mi_NextInputRow As Integer
Dim ma_SourceBooks() As String
Dim ma_SourceSheets() As Worksheet
Dim mi_NumberOfSheets As Integer
Sub PopulateSPCData()
'Get and set active workbook
Set mwb_Active = Workbooks("2022-02-04 SPC Report Template.xlsm")
mwb_Active.Activate
Set mws_Active = mwb_Active.Sheets("Raw Data")
mws_Active.Activate
'Find the next blank cell in template colA
For Each cell In mws_Active.Columns(1).Cells
'Set variable to the row number of the blank cell
If IsEmpty(cell) = True Then mi_NextInputRow = cell.Row: Exit For
Next cell
'The next line number is the blank row (- 1 for header)
mws_Active.Cells(mi_NextInputRow, 1).Value = mi_NextInputRow - 1
Debug.Print mi_NextInputRow
'Set up cells using custom
Dim xCBDACell As New DataCell
xCBDACell.SetSearchData = "CBDA"
xCBDACell.SetDestinationCell = mws_Active.Cells(mi_NextInputRow, 2)
Debug.Print xCBDACell.DestinationCell.Address
Dim xCBDCell As New DataCell
xCBDACell.SetSearchData = "CBD"
xCBDACell.SetDestinationCell = mws_Active.Cells(mi_NextInputRow, 3)
Dim xD9THCCell As New DataCell
xCBDACell.SetSearchData = "d9-THC"
xCBDACell.SetDestinationCell = mws_Active.Cells(mi_NextInputRow, 4)
Dim xD9THCACell As New DataCell
xCBDACell.SetSearchData = "d9-THCA"
xCBDACell.SetDestinationCell = mws_Active.Cells(mi_NextInputRow, 5)
'Get names of open workbooks
mi_NumberOfSheets = 0
i = 1
For i = 1 To Windows.Count
'If the book name includes this string
If Workbooks(i).Name Like "*SYSSU*" Then
Debug.Print Workbooks(i).Name
ReDim ma_SourceBooks(i)
ma_SourceBooks(i) = Workbooks(i).Name
ReDim ma_SourceSheets(i)
Set ma_SourceSheets(i) = Workbooks(ma_SourceBooks(i)).Sheets("Page 1") 'if this doesnt work
mi_NumberOfSheets = mi_NumberOfSheets + 1 'why does this work?
End If
Next i
If ma_SourceSheets(1) Is Nothing Then 'tried all numbers, all nothing
Debug.Print "No Source Sheet"
Else: Debug.Print mi_NumberOfSheets
End If
Dim xSumResString As String
Dim xSumResRow As Integer
xSumResString = "Summary Results:"
xSumResRow = 0
i = 1
For i = 1 To mi_NumberOfSheets
For Each cell In ma_SourceSheets(i).Columns(2).Cells
If cell.Value = xSumResString Then xSumResRow = cell.Row: Exit For
Next cell
Next i
Debug.Print xSumResRow
Debug.Print 'xResultsCol
'Set xCBDAAvgLoc = ma_SourceSheets(i).Range(xResultsRow, xResultsCol)
'Debug.Print xCBDAAvgLoc.Address
'Debug.Print ma_SourceSheets(i).Cells(xCBDAAvgLoc).Value
'mws_Active.Cells(xCBDACell).Value = ma_SourceSheets(i).Cells(xCBDAAvgLoc).Value
End Sub
In debug log (everything is what i want until No Source):
- 42 $B$42 2022-02-03 - SEQ-442 SYSSU.xlsx testcopy(2022-02-03 - SEQ-442 SYSSU).xlsx No Source Sheets
Thanks, let me know if i did anything wrong/need more info
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
