If, And, ElseIf, Else Statement Help

RMXByker

New Member
Joined
Apr 1, 2010
Messages
38
Hello all, back at it again with an easy question for the experts out there. I need the following to happen;

If F7 does not equal blank, and F10 does equal blank, then L10 should be filled with the text "3) Remove Shunt"
If F7 does not equal blank, and F10 does not equal blank, then L10 should be filled with the text "3) Remove 1st Stage Shunt"
Lastly, If F7 equals blank, then L10 equals blank.

I've attached my current code which is not functional. It is placed in the Worksheet Change structure but I'm still learning this stuff and I'm certainly in a bit deeper than my knowledge currently. The failure it's giving is nondescript in my eyes as it just gives a "out of stack space" or it just crashes Excel. Sorry I can't give more information on this.

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)    
        If "$F$7" <> "" And "$F$10" = "" Then
            Range("$L$10").Value = "3) Remove Shunt"
            
        ElseIf "$F$7" <> "" And "$F$10" <> "" Then
            Range("$L$10").Value = "3) Remove 2nd Stage Shunt"
        
        Else
            Range("$L$10").Value = """"
            
            
        End If
            
End Sub

Thank you...
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
A few things:

This is invalid syntax:
Code:
If "$F$7" <> "" And "$F$10" = "" Then
It should be:
Code:
If Range("$F$7") <> "" And Range("$F$10") = "" Then

Secondly, Worksheet_Change event procedure usually run upon manual data entry into specific cells, and the "Target" cell range is usually referenced in your VBA code.
Are you trying to run this on existing data, or as data is being entered?
What should be the triggering mechanism for this to run?
 
Upvote 0
Thanks for your input. I'll update the syntax. Ideally this would be triggered whenever there is a change to either F7 or F10. I'm just not sure how to set the trigger for either of those 2 cells.
 
Upvote 0
Code has been updated to below;

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)    
        If Range("$F$7") <> "" And Range("$F$10") = "" Then
            Range("$L$10").Value = "3) Remove Shunt"
            
        ElseIf Range("$F$7") <> "" And Range("$F$10") <> "" Then
            Range("$L$10").Value = "3) Remove 2nd Stage Shunt"
        
        Else
            Range("$L$10").Value = ""
            
            
        End If
            
End Sub

Now I'm getting a "Out of Stack Space" issue on the first line of code if I try to add a value to F7 and then I get a "Method 'Value' of object 'Range' failed after a few seconds when I try to delete that value out.
 
Upvote 0
You want to write it so that it only runs when F7 or F10 are manually updated, like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   Exit if more than one cell updated at once
    If Target.Count > 1 Then Exit Sub

'   Check to see if cell F7 or F10 updated
    If Target.Address(0, 0) = "F7" Or Target.Address(0, 0) = "F10" Then
        If Range("F7") <> "" Then
            If Range("F10") = "" Then
                Range("L10").Value = "3) Remove Shunt"
            Else
                Range("L10").Value = "3) Remove 2nd Stage Shunt"
            End If
        Else
            Range("L10").Value = ""
        End If
    End If

End Sub
 
Upvote 0
Thank you Joe. Works flawlessly. I will work on reading an understanding the additions you made so I don't make the mistake again. Thanks again for your time!
 
Upvote 0
You are welcome.

If you don't understand any part of the code or have any questions, feel free to ask away!
 
Upvote 0

Forum statistics

Threads
1,223,632
Messages
6,173,472
Members
452,516
Latest member
archcalx

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