Simplify my code please!

GiraffetheGeek

Board Regular
Joined
May 25, 2011
Messages
58
Hi all,

I have the following:
Code:
If  r = 0 Or r = 11 Or r = 12 Then
     r = r + 1
Else
     r = r + 2
End If

This works fine, but I want to extend it to IF r = any multiple of 11 up to 300.

Is there any way of doing this without manually typing r = 11 Or r = 22 etc.

Is there any limit to how many OR's VBA can check?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You can use the Mod command, which works the same as the =MOD worksheet function, but with different syntax than the worksheet function:

Code:
If r Mod 11 = 0 And r <= 300 Then 
     'Your code
 
Upvote 0
Hi there,

Yes, there's a limit (same as its worksheet function counterpart) of 30 for AND and OR statements.

That said, try this...

Code:
If r = 0 Or r >= 11 And r <= 300 Then
        r = r + 1
    Else
        r = r + 2
    End If

...or this:

Code:
Select Case r
        Case 0, 11 To 300
            r = r + 1
        Case Else
            r = r + 2
    End Select

HTH

Robert
 
Last edited:
Upvote 0
Robert,

If I read your one correctly then if r = 0 or and any number 11-300 the r would be increased by 1, else it would be increased by 2.

What I want is if r = 0, 11, 22, 33 or any multiple of 11 less then 300 then I want r increased by 1 else any other number r needs to be increased by 2

Sorry after just typing that last paragraph I noticed I made an error typing in my code in my first post. The 12 should be 22.

CWatts - I am just about to try yours.
 
Upvote 0
Sorry my bad - I didn't realise you needed multiples.

As well as CWatts's clever solution, here's a slightly different way:

Code:
If (r / 11 = CInt(r / 11)) And r <= 300 Then
        r = r + 1
    Else
        r = r + 2
    End If

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,220
Members
452,895
Latest member
BILLING GUY

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