PDA

View Full Version : [SOLVED] Help with If...Then



wnazzaro
06-29-2005, 09:09 AM
Hello again. Let's start with my code that is giving me problems.
'A
dd new numbers for RecruitMeetLoad.
If Month(LPFVDate) = 1 Or 4 Then
RecruitMeetLoad = TxLoad
ElseIf Month(LPFVDate) = 7 Or 10 Then
RecruitMeetLoad = TxLoad
ElseIf Month(LPFVDate) = 2 Or 5 Then
RecruitMeetLoad = (RecruitLoad + TxLoad) / 2
ElseIf Month(LPFVDate) = 8 Or 11 Then
RecruitMeetLoad = (RecruitLoad + TxLoad) / 2
Else
RecruitMeetLoad = RecruitLoad
End If
Cells(Target.row, LPFVCol) = RecruitMeetLoad
When I watch this in Break mode it goes from the first line to the second to End If which would be fine except Month(LPFVDate) = 2. This should go from the first line to the fifth or sixth, not the second.:dunno

wnazzaro
06-29-2005, 09:22 AM
This works


Public Sub Test()
'Add new numbers for RecruitMeetLoad.
Dim LPFVDate As Date
LPFVDate = Cells(1, 1)
Dim RecruitMeetLoad As Variant, TxLoad As Variant, RecruitLoad As Variant
TxLoad = 5
RecruitLoad = 2.6
If Month(LPFVDate) = 1 Then
RecruitMeetLoad = TxLoad
ElseIf Month(LPFVDate) = 4 Then
RecruitMeetLoad = TxLoad
ElseIf Month(LPFVDate) = 7 Then
RecruitMeetLoad = TxLoad
ElseIf Month(LPFVDate) = 10 Then
RecruitMeetLoad = TxLoad
ElseIf Month(LPFVDate) = 2 Then
RecruitMeetLoad = (RecruitLoad + TxLoad) / 2
ElseIf Month(LPFVDate) = 5 Then
RecruitMeetLoad = (RecruitLoad + TxLoad) / 2
ElseIf Month(LPFVDate) = 8 Then
RecruitMeetLoad = (RecruitLoad + TxLoad) / 2
ElseIf Month(LPFVDate) = 11 Then
RecruitMeetLoad = (RecruitLoad + TxLoad) / 2
Else
RecruitMeetLoad = RecruitLoad
End If
Cells(2, 2) = RecruitMeetLoad
End Sub

but it's a bit unwieldy. Is there a better way?

I guess this is more a problem with Month(Date) then If. Or maybe Or.

Norie
06-29-2005, 09:26 AM
Well for a start you have the wrong syntax for your If/Or.

This


If Month(LPFVDate) = 1 Or 4 Then
should be this


If Month(LPFVDate) = 1 Or Month(LPFVDate) = 4 Then

Personally I would use a Select Case structure.


Select Case Month(LPFVDate)
Case 1, 4, 7, 10
RecruitMeetLoad = TxLoad
Case 2, 5, 8, 11
RecruitMeetLoad = (RecruitLoad + TxLoad) / 2
Case Else
RecruitMeetLoad = RecruitLoad
End Select

wnazzaro
06-29-2005, 09:44 AM
Thank you.