Consulting

Results 1 to 4 of 4

Thread: Help with If...Then

  1. #1
    VBAX Regular
    Joined
    Feb 2005
    Posts
    82
    Location

    Help with If...Then

    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.

  2. #2
    VBAX Regular
    Joined
    Feb 2005
    Posts
    82
    Location
    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.

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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

  4. #4
    VBAX Regular
    Joined
    Feb 2005
    Posts
    82
    Location
    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •