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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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