Hey,
I have a dataset filled from A1 to E35383. A contains dates, but only the first of each month, and it switches months every few thousand rows. There is a certain term in column B, let's say "Pretzels," that appears once in about 4 of these months in non-consecutive months. In other words, I have data for Pretzels for May, January, and December, but not any of the other months. I need to add in a line for Pretzels for the months where it is missing, and just have the corresponding C, D, and E cells say "0" to denote that there isn't data for Pretzels for that month. Here's what I've been thinking for the code so far, but it seems very inefficient and obviously it doesn't work, but it might help you get the idea for what I'm trying to accomplish:
Sub pretzels()
With Sheet1
Dim founddates As Long
Dim missingdates As Long
Dim totaldates As Range
Dim Rowcount As Long
Dim Lastrow As Long
totaldates = 6/1/2009 to 5/1/2010
Lastrow = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Rowcount = 1
For Rowcount = 1 To Lastrow
If .Cells(Rowcount, 2).Value Like "*pretzels*" Then
founddates = .Cells(Rowcount, 1).Value
End If
Next Rowcount
End With
missingdates = founddates - totaldates
For Rowcount = 1 To Lastrow
If .Cells(Rowcount, 1).Value Like "*missingdates*" And .Cells(Rowcount, 2).Value Like "*Muffins*" Then
'Muffins is a value in B that appears once and only once for each month, so we can add in missing Pretzel data below it safely
.Cells(Rowcount + 1, 2).Select
Selection.EntireRow.Insert
.Cells(Rowcount, 2).Offset(1, 0).Value = "pretzels"
.Cells(Rowcount, 2).Offset(1, -1) = ActiveCell.Offset(-1, -1).Value
.Cells(Rowcount, 2).Offset(1, 1) = "0"
.Cells(Rowcount, 2).Offset(1, 2) = "0"
.Cells(Rowcount, 2).Offset(1, 3) = "0"
End If
Next Rowcount
End Sub
If this is the most efficient way to go, then I'd need help getting the missingdates = totaldates - founddates piece to work, I think. If anyone has another solution (which I'm sure exists), please throw it out there. Thanks for the help.
Bookmarks