Hello all, VBA newbie here and thanks in advance. I've got a series of worksheets with a date at E3 on each sheet. My VBA code is for CompareMonth from 1 to 12, for each worksheet, see if the month of the date in E3 matches CompareMonth. If yes, copy the value in cell F29 and F30 to two values, G and P, else next worksheet, then next CompareMonth. My problem is that the value of the date that gets returned on each worksheet is 12:00:00 AM so my code never gets to yes, values G and P never get added. Any clues for me? I have put in MsgBox's so I get some visual clue as to what is happening: ActualDate keeps giving a value of 12 and then there is no value for G or P. Sorry it's so long, I don't know what I'm doing wrong, can't find anything in reference manuals or online as to why I can't get month out of the date value.
[VBA]Dim skw As Integer
Dim CompareMonth As Integer
Dim w As Integer
Dim G As Currency
Dim P As Currency
Dim FindDate As Date
Dim ActualDate As Integer
Dim testDate As Date
G = 0
P = 0
CompareMonth = 1
w = 2
skw = Worksheets.Count
MsgBox (skw)
For CompareMonth = 1 To 12
For w = 2 To skw
MsgBox ("skw is ") & skw 'this works
Worksheets(w).Activate
MsgBox ("date is ") & Range("E3").Value 'gives a blank here
testDate = Cells(5, 3).Value
testDate = Format(testDate, "dd/mm/yyyy")
FindDate = DateValue(testDate)
MsgBox (FindDate)
ActualDate = DatePart("m", FindDate)
MsgBox (w) & " " & ActualDate 'ActualDate gives 12 always
If CompareMonth = ActualDate Then
MsgBox "The month: " & ActualDate 'only reach here when CompareMonth is 12
G = G + Range("F29").Value
P = P + Range("F30").Value
Else
MsgBox ("Nothing")
End If
Next w
Worksheets(1).Select
Cells(CompareMonth + 2, 2).Value = G
Cells(CompareMonth + 2, 3).Value = P
MsgBox (CompareMonth)
G = 0
P = 0
Next CompareMonth
MsgBox ("Done")
End Sub
[/VBA]
Thanks all.