how to ignore range within defined range

xcel50

New Member
Joined
Mar 19, 2015
Messages
25
Hello

I have a defined range of 16 LED's but I want (i) to ignore the LED's 13-15 but still do 1-12 and 16 how can I do this?

If Sheets("Intro").Range("CHOOSE_LED").Value = "CLASSIFIED" Then
For i = 1 To 16 '16 LEDs
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You can handle this with the Select Case statement:

Code:
If Sheets("Intro").Range("CHOOSE_LED").Value = "CLASSIFIED" Then
    For i = 1 To 16 '16 LEDs
        Select Case i
            Case 1 - 12, 16
                'Do things
            Case 13 - 15
                'Do nothing
        End Select
    Next i
End If
 
Last edited:
Upvote 0
You could just add goto code inside the loop to skip those numbers

For i = 1 To 16 '16 LEDs
If i >= 13 And i <= 15 Then GoTo skipCode


'''main coding here

skipCode:
next i
 
Upvote 0
You could just add goto code inside the loop to skip those numbers
Best practice usually says to avoid GoTo statements, if you can.
However, you can still employ your logic pretty easily without them, like this:
Code:
[COLOR=#333333][COLOR=#333333]For i = 1 To 16 '16 LEDs
[/COLOR][/COLOR][COLOR=#333333]    If i >= 13 And i <= 15 Then[/COLOR][COLOR=#333333][COLOR=#333333]
    Else
''       'main coding here
    End If
next i[/COLOR][/COLOR]
 
Last edited:
Upvote 0
Thanks Joe , what's the reasoning behind avoiding goto statements? I use them quite a lot in some of my longer code to skip around :(
 
Upvote 0
Thanks Joe , what's the reasoning behind avoiding goto statements? I use them quite a lot in some of my longer code to skip around
I think that is precisely the issue; creating what is referred to as "spaghetti code", that can be really hard to follow and debug.

I came across a few discussions via Google on it; though there are certainly others:
10 ways to screw up your VBA code - TechRepublic

This one is interesting. The person wrote an article defending the use of GoTo, but if you read some of the comments posted at the bottom, they detail some of the concerns with it.
Are GOTO Statements Still Dirty? – Bacon Bits

Basically, I try to operate under the mantra of only using them when nothing else will work. As such, I only ever use them in error handling.
 
Upvote 0
Best practice usually says to avoid GoTo statements, if you can.
However, you can still employ your logic pretty easily without them, like this:
Code:
[COLOR=#333333][COLOR=#333333]For i = 1 To 16 '16 LEDs
[/COLOR][/COLOR][COLOR=#333333]    If i >= 13 And i <= 15 Then[/COLOR][COLOR=#333333][COLOR=#333333]
    Else
''       'main coding here
    End Id
next i[/COLOR][/COLOR]

Im not an expert on vba but the code works as per Joe4 suggestions but if try your code it doesn't I get a compile error after I replace the word "skipCode:" for "End Id" "Compile error: Expected: If or Select or Sub or Function or Property or Type or With or Enum or end of staement"
 
Upvote 0
Sorry, I had a typo.
It should be "End If", not "End Id".
I went back and updated my original reply.
 
Upvote 0
Sorry, I had a typo.
It should be "End If", not "End Id".
I went back and updated my original reply.


:( now I get a "Next without For" , I have tried different things but its not working, worst part is I cant upload of the code. I think I'll stick to Mr. Tenny respond
 
Upvote 0

Forum statistics

Threads
1,224,942
Messages
6,181,900
Members
453,068
Latest member
DCD1872

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