Hi,
The code, below, is used to find a list of start and end dates from a matrix grid. I have been able to save the values in an array and output the results... however the results are weird. Each loop should output pairs of dates but its adding in lots of commas in the successive columns. I'm guessing that the array never really clears and the new values are simply appending onto the end. I can't seem to get rid of this.
The results should output like this :
01/01/2018,05/01/2018,15/02/2018,19/02/2018
05/01/2018,31/01/2018
10/02/2018,11/02/2018
etc...
however it's returning
01/01/2018,05/01/2018,15/02/2018,19/02/2018
,,,,05/01/2018,31/01/2018
,,,,,,10/02/2018,11/02/2018
each row with preceding commas which I'm guessing represent an empty dimension of the array MyDay ?
I would welcome any advice on how to improve my code or solve this problem
Kind Regards
The code, below, is used to find a list of start and end dates from a matrix grid. I have been able to save the values in an array and output the results... however the results are weird. Each loop should output pairs of dates but its adding in lots of commas in the successive columns. I'm guessing that the array never really clears and the new values are simply appending onto the end. I can't seem to get rid of this.
Code:
Sub FindDates()
Dim DATA As Worksheet: Set DATA = Worksheets("New Data")
Dim LastRow, LastCol As Long
Dim x, y As Long
Dim MyDay() As Variant
DATA.Select
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
'PROCESS EACH CELL IN THE CHOSEN ROW AND DETERMINE IF IT IS A START OR END DATE
For MyRow = 2 To LastRow
DATA.Range(Cells(MyRow, 11), Cells(MyRow, LastCol)).Select
For Each rCell In Selection
'IF THE WHOLE ROW IS NEEDED
If WorksheetFunction.CountA(Range(Cells(MyRow, 11), Cells(MyRow, LastRow))) = WorksheetFunction.CountA(Range(Cells(1, 11), Cells(1, LastRow))) Then
ReDim Preserve MyDay(x)
MyDay(x) = Cells(1, rCell.Column).Value
x = x + 1
ReDim Preserve MyDay(x)
MyDay(x) = Cells(LastRow, rCell.Column).Value
x = x + 1
GoTo NextRow
End If
'CHECK IF THE FIRST VALUE IS A START DATE
If IsEmpty(rCell.Value) = False And IsEmpty(rCell.Offset(0, 1).Value) = False And rCell.Column = 11 Then
ReDim Preserve MyDay(x)
MyDay(x) = Cells(1, rCell.Column).Value
x = x + 1
End If
'IF CELL VALUE IS NOT EMPTY AND THE VALUE BEFORE AND AFTER IS EMPTY = SINGLE DAY (IGNORED)
If IsEmpty(rCell.Value) = False And IsEmpty(rCell.Offset(0, 1).Value) = True And IsEmpty(rCell.Offset(0, -1).Value) = True Then
GoTo NextCell
End If
'IF CELL VALUE IS NOT EMPTY AND THE VALUE BEFORE IS EMPTY = FIRST DAY
If IsEmpty(rCell.Value) = False And IsEmpty(rCell.Offset(0, -1).Value) = True Then
ReDim Preserve MyDay(x)
MyDay(x) = Cells(1, rCell.Column).Value
x = x + 1
End If
'IF CELL VALUE IS NOT EMPTY AND THE VALUE AFTER IS EMPTY = LAST DAY
If IsEmpty(rCell.Value) = False And IsEmpty(rCell.Offset(0, 1).Value) = True Then 'CELL IS NOT BLANK AND NEXT CELL IS EMPTY
ReDim Preserve MyDay(x)
MyDay(x) = Cells(1, rCell.Column).Value + 6
x = x + 1
End If
NextCell:
Next rCell
NextRow:
DATA.Range("BL" & MyRow).Value = Join(MyDay, ",")
ReDim MyDay(0) As Variant
Next MyRow
End Sub
The results should output like this :
01/01/2018,05/01/2018,15/02/2018,19/02/2018
05/01/2018,31/01/2018
10/02/2018,11/02/2018
etc...
however it's returning
01/01/2018,05/01/2018,15/02/2018,19/02/2018
,,,,05/01/2018,31/01/2018
,,,,,,10/02/2018,11/02/2018
each row with preceding commas which I'm guessing represent an empty dimension of the array MyDay ?
I would welcome any advice on how to improve my code or solve this problem
Kind Regards