VBA for If & Else

bademployee

Board Regular
Joined
Aug 19, 2010
Messages
184
Hi all,

Currently using:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("b12") = "UAN" Then Range("z12") = "Variable"
End Sub

But I need to modify so if B12 contained any other word other than "UAN", I need Z12 to display "Fixed".

I'm unable to get the syntax/code correct.

Thanks for any help in advance.

Mark
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("b12") = "UAN" Then
        Range("z12") = "Variable"
    Else
        Range("z12") = "Fixed"
    End Sub
 
Upvote 0
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("b12") = "UAN" Then
        Range("z12") = "Variable"
    Else
        Range("z12") = "Fixed"
    End Sub

Doesn't seem to work.

I forgot to add b12 is actually b12:c12 (merged cells), would this cause an issue?

Also, if b12:c12 = "" then I need z12 to be "" also. Not sure how to add this.

Thanks
 
Upvote 0
No it wont be an issue. Sorry for the false syntax. Right syntax is:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("b12") = "UAN" Then
        Range("z12") = "Variable"
    ElseIf Range("b12") = "" Then
        Range("z12") = ""
    Else
        Range("z12") = "Fixed"
    End If
End Sub
 
Last edited by a moderator:
Upvote 0
No it wont be an issue. Sorry for the false syntax. Right syntax is:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("b12") = "UAN" Then
        Range("z12") = "Variable"
    ElseIf Range("b12") = "" Then
        Range("z12") = ""
    Else
        Range("z12") = "Fixed"
    End If
End Sub

Awesome, works perfectly.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,216,104
Messages
6,128,856
Members
449,472
Latest member
ebc9

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