Vba if, elseif

bsquad

Board Regular
Joined
Mar 15, 2016
Messages
194
Looking to see why this isn't working,
both x and y are Integers

I am really hoping this is simple, but I have this segment with nested in my code and when doing it step by step or F8 and when I run it normally its not working. Like if x = 8, it specifies y = 7 which is totally not right; and insight would be helpful - maybe I have the logic wrong I am not sure

I have a For x = 1 to worksheets.count thing going on above the code below
Code:
                            If x = 1 Then
                            y = 15
                                ElseIf x = 2 Then
                                y = 8
                                ElseIf x = 3 Or 4 Or 9 Then
                                y = 7
                                ElseIf x = 5 Or 7 Or 8 Or 10 Then
                                y = 3
                                ElseIf x = 8 Then
                                y = 12
                            End If
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You cannot do ORs like this:
Code:
ElseIf x = 3 Or 4 Or 9 Then
They need to be spelled out like this
Code:
ElseIf x = 3 Or x = 4 Or x = 9 Then

I would prefer to use Case statements, though:
Code:
    Select Case x
        Case 1
            y = 15
        Case 2
            y = 8
        Case 3, 4, 9
            y = 7
        Case 5, 7, 8, 10
            y = 3
        Case Else
            y = 12
    End Select
Also note that you have x=8 listed twice (in your last two checks). You will want to fix that.
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,453
Members
449,161
Latest member
NHOJ

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