VBA If statement not working

danjames11

New Member
Joined
Apr 22, 2009
Messages
14
I have made an If statement that, will make a date go 3 months on from the date you select on a drop down list, today, yesterday, 2 days ago, and 3 days ago. And when you click the button it changes the date on the cell 2 away from teh active cell. But for some reason the first 4 If statements work (Quarterly), and he last 4 (monthly) do not. Heres the code, can you help please?

Code:
Sub ClientPaid_Click()
If IsNumeric(Application.Match(ActiveCell.Value, Range("B13:B4000"), 0)) Then
If Range("D10") = "Quarterly" Then
If Range("L1") = "Today" Then
ActiveCell.Range("C1").Select
ActiveCell = Range("O6").Value
Range("L1") = "Today"
Else
If Range("D10") = "Quarterly" Then
If Range("L1") = "Yesterday" Then
ActiveCell.Range("C1").Select
ActiveCell = Range("O10").Value
Range("L1") = "Today"
Else
If Range("D10") = "Quarterly" Then
If Range("L1") = "2 days Ago" Then
ActiveCell.Range("C1").Select
ActiveCell = Range("R6").Value
Range("L1") = "Today"
Else
If Range("D10") = "Quarterly" Then
If Range("L1") = "3 days Ago" Then
ActiveCell.Range("C1").Select
ActiveCell = Range("R10").Value
Range("L1") = "Today"
Else
If Range("D10") = "Monthly" Then
If Range("L1") = "Today" Then
Range("C1").Select

Else
If Range("D10") = "Monthly" Then
If Range("L1") = "Yesterday" Then
ActiveCell.Range("C1").Select
ActiveCell = Range("O11").Value
Range("L1") = "Today"
Else
If Range("D10") = "Monthly" Then
If Range("L1") = "2 days Ago" Then
ActiveCell.Range("C1").Select
ActiveCell = Range("R7").Value
Range("L1") = "Today"
Else
If Range("D10") = "Monthly" Then
If Range("L1") = "3 days Ago" Then
ActiveCell.Range("C1").Select
ActiveCell = Range("R11").Value
Range("L1") = "Today"
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If


Else
MsgBox "You need to Click on a Client Name, before you can select them as paid.", vbOKOnly, "Time For You Message - Paid Client"
End If
End Sub

All the quarterly ones work, and the monthly ones do not. What have I done wrong?
 
Last edited by a moderator:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Probably has something to do with the logic somewhere in your maze of nested If statements. Seriously, that thing is *not* pretty. It looks like it will only check for the "Monthly" option if it manages to get through all of your "Quarterly" statements.

You could make things a lot easier to read/understand (not to mention better coding) if you used Select Case statements. Not tested, but should give you some idea of what's possible:
Code:
Sub ClientPaid_Click()


If IsNumeric(Application.Match(ActiveCell.Value, Range("B13:B4000"), 0)) Then
    Select Case Range("D10").Value
        Case Is = "Quarterly"
            Select Case Range("L1").Value
                Case "Today"
                    Range("C1") = Range("O6").Value
                    Range("L1") = "Today"
                Case "Yesterday"
                    Range("C1") = Range("O10").Value
                    Range("L1") = "Today"
                Case "2 days Ago"
                    Range("C1") = Range("R6").Value
                    Range("L1") = "Today"
                Case "3 days Ago"
                    Range("C1") = Range("R10").Value
                    Range("L1") = "Today"
            End Select
        Case Is = "Monthly"
            Select Case Range("L1").Value
                Case Is = "Today"
                    Range("C1").Select
                Case Is = "Yesterday"
                    Range("C1") = Range("O11").Value
                    Range("L1") = "Today"
                Case Is = "2 days Ago"
                    Range("C1") = Range("R7").Value
                    Range("L1") = "Today"
                Case Is = "3 days Ago"
                    Range("C1") = Range("R11").Value
                    Range("L1") = "Today"
            End Select
    End Select
Else
    MsgBox "You need to Click on a Client Name, before you can select them as paid.", vbOKOnly, "Time For You Message - Paid Client"
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top