If then vba

bsquad

Board Regular
Joined
Mar 15, 2016
Messages
194
this sounds dumb and can't really find what I am looking for online; but is there an alternative to wasting two code lines after THEN for this below. Is there some character>> I have also tried using AND between e and z - it doesn't work.

Code:
                            If x = 12 Or x = 13 Or x = 15 Or x = 16 Or x = 17 Or x = 18 Or x = 23 Or x = 25 Then
                                    z = 5
                                    e = 2
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
The number of lines used by a code is not a factor in that code's 'Quality'

That said, you can do this
Code:
z = 5: e = 2
Strictly speaking, that is still 2 lines of code. Just written in 1 line.


However, I would use a select case structure instead of the multiple ORs
Code:
Select Case x
    Case 12, 13, 15, 16, 17, 18, 23, 25
        z = 5
        e = 2
End Select
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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