This is a long shot, but I could really use some ideas...
I have a macro that works as follows:
- I grab a "start date" and "end date" from an input cell.
- I have an array that stores the name of various worksheets.
- For each element in this array, I search another workbook to see if it contains a worksheet with that name, and if so, I identify the column in that worksheet that contains the string "Date".
- I loop through each row in this column that contains a cell (date) between the start date and end date specified, and append each value (date) to a collection (to store a list of de-duplicated dates).
- I add the de-duped list of dates to a two-dimensional array called "datesArray". The first element stores the worksheet I referenced (or rather, a value incremented each time I access a new worksheet), and the second element stores the unique dates I found between the start and end date indicated.
- Worth mentioning, the second dimension in my 2D array is constantly redimensioned to be equal to the maximum of any collection of dates from any worksheet looked at so far.
- I repeat this process for each worksheet, until I have all the dates I need.
- At a later point in my code, I need to essentially iterate through the list of dates I have for each worksheet and create rows with values I pull from other places in my input data. Basically I need to change the output slightly every time a new date is found in a worksheet (between the start/end dates specified).
- ^^^^ One check I have in place is if a particular element in the second dimension of the date array is empty (e.g. signifying I've gone through all dates for a particular worksheet but there were other worksheets with more date values) AND the preceding second dimension element was not empty (e.g. signifying the last date in the worksheet) AND the month of this date is less than the month of the end date specified (e.g. the last date was 10/29 but the end date specified was 11/15), then I need to apply a "Default" condition for my output data equal to the first day of the month of the end date specified.
My code looks something like this:
Note:
- wb2ws : Reference to worksheet in workbook
- wb2wsdaterownum : row where the string "Date" appears (e.g. 10)
- wb2wsdatecollet : letter of column where the string "Date" appears (e.g. B)
- wb2ws_lastrow : last row in worksheet (e.g. 57)
- wb2wsdate : date value (e.g. 10/1/2019 or vbNull/1 if not a date or adjacent cell in same row is empty)
- dateCollection : should store unique dates only that fall between the start and end dates specified (e.g. 11/1/2019, 11/3/2019, etc.)
- datesArray : 2D array to store unique dates from the collection (because I later empty it) against a particular count of worksheets referenced (so I can differentiate which worksheet the dates came from)
- start_date & end_date : e.g. 10/20/2019 and 11/21/2019)
- end_month : month of end_date (e.g. 11)
- colcount (collection count) : stores the length of the second dimension of the date array
- maxcolcount: stores the maximum length of the second dimension in my date array (e.g. the largest amount of unique dates I found in a worksheet).
- count: keeps track of how many times I needed to access a worksheet, and is incremented to store that unique worksheet reference's dates.
- output_Array: contains a bunch of values from an input sheet.
- output_Array2: output_Array modified to incorporate the date values.
- count2: keeps track of the amount of rows in my new output.
Code:
For Each s In wb2ws.Range("A"& wb2wsdaterownum & ":"& wb2wsdatecollet & wb2ws_lastrow)
If (IsDate(s.Offset(, wb2wsdatecolnum - 1).Value) And s.Offset(, wb2wsskucolnum - 1).Value <> "") Then
wb2wsdate = s.Offset(, wb2wsdatecolnum - 1).Value
Else
wb2wsdate = vbNull
End If
On Error Resume Next
If wb2wsdate <> vbNull And wb2wsdate >= start_date And wb2wsdate <= end_date Then
dateCollection.Add wb2wsdate, CStr(wb2wsdate)
End If
Next s
For Each ss In dateCollection
datesArray(v, colcount) = ss
colcount = colcount + 1
If colcount > maxcolcount Then
maxcolcount = colcount
ReDim Preserve datesArray(count, maxcolcount)
End If
Next ss
For t = 0 To maxcolcount
For u = 0 To count
****If (t > 0 And datesArray(u, t) = "" And datesArray(u, t - 1) <> "" And Month(datesArray(u, t - 1)) < end_month) Then
output_Array2(0, count2) = "default"
output_Array2(1, count2) = output_Array(1, u)
output_Array2(2, count2) = output_Array(2, u)
output_Array2(3, count2) = Format(end_month & "/01/"& end_year, "m/d/yyyy hh:mm:ss AM")
output_Array2(4, count2) = output_Array(3, u)
output_Array2(5, count2) = output_Array(4, u)
output_Array2(6, count2) = output_Array(5, u)
output_Array2(7, count2) = output_Array(6, u)
output_Array2(8, count2) = output_Array(7, u)
count2 = count2 + 1
ReDim Preserve output_Array2(8, count2)
End If
If datesArray(u, t) <> "" Then
output_Array2(0, count2) = output_Array(0, u) & "_"& CStr(Format(datesArray(u, t), "mmddyy"))
output_Array2(1, count2) = output_Array(1, u)
output_Array2(2, count2) = output_Array(2, u)
output_Array2(3, count2) = Format(datesArray(u, t), "m/d/yyyy hh:mm:ss AM")
output_Array2(4, count2) = output_Array(3, u)
output_Array2(5, count2) = output_Array(4, u)
output_Array2(6, count2) = output_Array(5, u)
output_Array2(7, count2) = output_Array(6, u)
output_Array2(8, count2) = output_Array(7, u)
count2 = count2 + 1
ReDim Preserve output_Array2(8, count2)
End If
Next u
Next t
The problem is with the conditional I indicated with **** in my code.
It should only get executed under the conditions specified in my bullet list next to ^^^^.
So if:
- start_date = "10/20/2019"
- end_date = "11/20/2019"
- end_month = 11
- datesArray(51, 0) = "10/20/2019",
- datesArray(51, 1) = "10/27/2019"
- datesArray(51, 2) = ""
Then my **** should execute because there is no date collected in the month of the end date, and it needs a default row.
The problem is I'm seeing that my conditional is executed when t = 0, e.g. the first date is accessed, even though I explicitly mentioned that t must be greater than 0 (among other conditions). This basically means the default row is getting added for every worksheet after the first date is found, which it shouldn't!
I realize my post is long (sorry) and I don't really expect anyone to fix my code persay (my actual code is too long and proprietary to post entirely), but I was hoping someone could provide me with some ideas on a high level, based on what I described, regarding why my conditional is not working in the case where t = 0.
Thank you!