Sub WriteNewProductsInfo()
Dim rng As Range
Dim cel As Range
Dim lastRow As Long
Dim writeRow As Long
Dim firstDateColumn As Long
Dim firstDate As Date
lastRow = Sheets("Products By Qty Pivot").Cells(Rows.Count, "AG").End(xlUp).Row
writeRow = Sheets("NewProducts").Cells(Rows.Count, "A").End(xlUp).Row + 1
With Sheets("Products By Qty Pivot")
Set rng = .Range("AG5:AG" & lastRow)
For Each cel In rng
If cel.Value = 1 Then
'Write New Product Code from Pivot to NewProducts tab
Sheets("NewProducts").Range("A" & writeRow).Value = cel.Offset(0, -32).Value
'Count columns until first non-blank cell. If the first column isn't empty, it's the first date.
If IsEmpty(Sheets("Products By Qty Pivot").Range("B" & cel.Row)) = False Then
firstDateColumn = 2
Else
firstDateColumn = .Cells(1, .Range("A" & cel.Row).End(xlToRight).Column).Column
End If
'Find the date in the header row 4 based on the column number
firstDate = .Cells(4, firstDateColumn).Value
'Write date based on column number
Sheets("NewProducts").Range("B" & writeRow).Value = firstDate
writeRow = writeRow + 1
End If
Next cel
End With
End Sub