On Aug 13, 10:29 am, thuyh...@gmail.com wrote:
> Hello everyone -
>
[quoted text clipped - 27 lines]
> I also posted this question on [http://www.mrexcel.com/board2/
> viewtopic.php?t=287266]
Here's a possibility for you. You have to assign this macro to a
button click, and then click the button only after selecting the top
date in your ColumnA. It will walk down the column filling in missing
zeroes for two companies, "CompA" and "CompB" You'll want to change
these constants to match your situation.
Private Sub FillDate_Click()
Dim MyDate As Date
Dim MyRow As Integer
Dim MyCol As Integer
Const CompA = "CompA"
Const CompB = "CompB"
Dim CompAPresent As Boolean
Dim CompBPresent As Boolean
Dim ExpectedDate As Variant
Dim LastDateSeen As Variant
MyRow = Selection.Row
MyCol = Selection.Column
LastDateSeen = 0
CompAPresent = False
CompBPresent = False
While IsDate(ActiveSheet.Cells(MyRow, MyCol).Value)
' detect if this is the same date we saw last time through
If LastDateSeen <> 0 _
And LastDateSeen <> ActiveSheet.Cells(MyRow, MyCol).Value Then
CompanyCheck:
' upon seeing a new date, check that we had found values
for
' each company on the prior date
If Not CompAPresent Then
ActiveSheet.Rows(MyRow).Insert
ActiveSheet.Cells(MyRow, MyCol).Value = LastDateSeen
ActiveSheet.Cells(MyRow, MyCol + 1).Value = CompA
ActiveSheet.Cells(MyRow, MyCol + 2).Value = 0
MyRow = MyRow + 1
End If
If Not CompBPresent Then
ActiveSheet.Rows(MyRow).Insert
ActiveSheet.Cells(MyRow, MyCol).Value = LastDateSeen
ActiveSheet.Cells(MyRow, MyCol + 1).Value = CompB
ActiveSheet.Cells(MyRow, MyCol + 2).Value = 0
MyRow = MyRow + 1
End If
' detect if this new date is the next chronological date
ExpectedDate = DateSerial(Year(LastDateSeen),
Month(LastDateSeen), Day(LastDateSeen) + 1)
If ActiveSheet.Cells(MyRow, MyCol).Value <> ExpectedDate
Then
CompAPresent = False
CompBPresent = False
LastDateSeen = ExpectedDate
GoTo CompanyCheck
End If
CompAPresent = False
CompBPresent = False
End If
' detect company on current record
If ActiveSheet.Cells(MyRow, MyCol + 1).Value = CompA Then
CompAPresent = True
If ActiveSheet.Cells(MyRow, MyCol + 1).Value = CompB Then
CompBPresent = True
NextLine:
' remember the date and move to next row
LastDateSeen = ActiveSheet.Cells(MyRow, MyCol).Value
MyRow = MyRow + 1
Wend
If LastDateSeen <> 0 Then
If Not CompAPresent Then
ActiveSheet.Rows(MyRow).Insert
ActiveSheet.Cells(MyRow, MyCol).Value = LastDateSeen
ActiveSheet.Cells(MyRow, MyCol + 1).Value = CompA
ActiveSheet.Cells(MyRow, MyCol + 2).Value = 0
MyRow = MyRow + 1
ElseIf Not CompBPresent Then
ActiveSheet.Rows(MyRow).Insert
ActiveSheet.Cells(MyRow, MyCol).Value = LastDateSeen
ActiveSheet.Cells(MyRow, MyCol + 1).Value = CompB
ActiveSheet.Cells(MyRow, MyCol + 2).Value = 0
MyRow = MyRow + 1
End If
End If
End Sub
HTH
Brian Herbert Withun