Macro to check a Cells Value and run different code depending on the cells value?

Event2020

Board Regular
Joined
Jan 6, 2011
Messages
122
Office Version
  1. 2019
Platform
  1. Windows
Hi
I have the following code in a macro that creates sets the value of Cell B25.

Code:
If Range("B23").Value = 2 Or Range("B23").Value = 3 Or Range("B23").Value = 4 Or Range("B23").Value = 5 Then run code

<run code="">This works well but I now need it to do the following so it runs different code depending on the value of Cell B23:

Code:
If Range("B23").Value = 1 Then run code A
        <run code="">
but

If Range("B23").Value = 2 Then run code B
        <run code="">
but

If Range("B23").Value = 3 Then run code C
        <run code="">
but

If Range("B23"). Value = 4 Or Range("B23").Value = 5 Then run code D
<run code="">

I am learning so much from this forum and you helpful folks but my abilities have stalled on this?</run></run></run></run></run>
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Here you go

Code:
If Range("B23").Value = 1 Then run code A
ElseIf Range("B23").Value = 2 Then run code B
ElseIf Range("B23").Value = 3 Then run code C
ElseIf Range("B23"). Value = 4 Or Range("B23").Value = 5 Then run code D
End If
 
Upvote 0
Here you go

Code:
If Range("B23").Value = 1 Then run code A
ElseIf Range("B23").Value = 2 Then run code B
ElseIf Range("B23").Value = 3 Then run code C
ElseIf Range("B23"). Value = 4 Or Range("B23").Value = 5 Then run code D
End If


Thank you so much

Why on earth I could not work that out myself or find it on google I dont know.

You have really helped me out.

Thanks again.
 
Upvote 0
Another way, especially useful when multiple values &/or range(s) of values are involved - see my second last 'Case' statement
Code:
Select Case Range("B23").Value
  Case 1: Run code A
  Case 2: Run code B
  Case 3: Run code C
  Case 4, 5: Run code D
  Case 6, 9 To 15, 20: Run code E
  Case Else: Run code F
End Select

End Select
 
Last edited:
Upvote 0
Another way, especially useful when multiple values &/or range(s) of values are involved - see my second last 'Case' statement
Code:
Select Case Range("B23").Value
  Case 1: Run code A
  Case 2: Run code B
  Case 3: Run code C
  Case 4, 5: Run code D
  Case 6, 9 To 15, 20: Run code E
  Case Else: Run code F
End Select

End Select

Hi Peter

Thank you for your reply.

On your 2nd last Case statement can you explain what that does (so I may learn)

Many thanks
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
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