Você está na página 1de 6

4/11/2019 excel - Loop through all numbers in a range except a few - Stack Overflow

The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in
the 2019 Developer Survey.

Loop through all numbers in a range except a few Ask Question

I need to loop through i = 1 to 99 but I


want to skip a few specific i values.
2 the numbers I want to skip are
41,83,87,91,92,93,98

I realize I could nest all my actions


inside an i <> 41 , or i <> 83 etc.
Isn't there an easier way? Maybe
assign a variable to contain values to
skip in a CSL and use Split ? I don't
know my brain isn't working. Please
help.

For i = 1 To 99
If i <> 41 And i <> 83 And i <> 87 And i <> 91 _
And i <> 92 And i <> 93 And i <> 98 Then
'do stuff
End If
Next i

Would prefer to set up a variable like:

not_use = "41,83,87,91,92,93,98"

then have some sort of For i = 1 To


99 unless in not_use but there is no
way to write that as far as I know.

excel vba excel-vba

edited Apr 16 '15 at 1:09


L42
16.8k 7 32 56

asked Apr 15 '15 at 22:16


Johnson Jason
372 1 7 21

What have you tried? Please can you


provide some code you have
attempted yourself. – Matt D. Webb
Apr 15 '15 at 22:18

added code I have tried. –


Johnson Jason Apr 15 '15 at 22:23

There are many answers to this now,


it would be a good idea to try them all
and maybe time them then mark the
winner as the accepted answer. –
Dan Donoghue Apr 15 '15 at 23:08
https://stackoverflow.com/questions/29661897/loop-through-all-numbers-in-a-range-except-a-few 1/6
So many good answers!!! Thanks
So many good answers!!! Thanks
4/11/2019 excel - Loop through all numbers in a range except a few - Stack Overflow
guys. I am not sure which one I will
choose just yet but I will check mark
one of these when I make up my
mind. Thanks again. –
Johnson Jason Apr 15 '15 at 23:12

6 Answers

Throw i substituted with nothing at


the string length and compare:
1
Sub LoopSkip()
Dim NotUse As String
NotUse = "41,83,87,91,92,93,98"
For i = 1 To 99
If Len("," & NotUse & ",") =
Then
'Do Stuff
End If
Next i
End Sub

edited Apr 15 '15 at 23:00

answered Apr 15 '15 at 22:37


Dan Donoghue
4,732 1 8 32

Clever but I have a question, when i


reaches the number 8 for example
wouldn't it find "8" in the string and
replace it with blank causing the
string lengths to not be equal and
then fail ? – Johnson Jason Apr 15
'15 at 22:48

It wraps them in commas to counter


this, it doesn't look for 8 in the string,
it looks for ,8, in the string which has
had a comma put on the start and
end :). – Dan Donoghue Apr 15 '15 at
22:52

If it "doesn't" find the number the


string length is the same which allows
it to enter the routine. –
Dan Donoghue Apr 15 '15 at 23:04

1 Winner, winner chicken dinner. I used


this. Thank you. I am glad I caught
that error and that after my first
comment you were able to change it
into something that would work for all
of my intended uses. Thanks again
for making this and thanks again for
the quick update and error fix. :) –
Johnson Jason Apr 17 '15 at 19:46

Glad I could help you :). –


Dan Donoghue Apr 20 '15 at 3:41

https://stackoverflow.com/questions/29661897/loop-through-all-numbers-in-a-range-except-a-few 2/6
4/11/2019 excel - Loop through all numbers in a range except a few - Stack Overflow

You can specify the values to ignore


in a more compact way than with your
6 If statements using Select Case :

For i = 1 To 99
Select Case i
Case 41, 83, 87, 91, 92, 93, 98
'Do nothing
Case Else
'Do stuff
End Select
Next

edited Apr 15 '15 at 22:57


pnuts
49.2k 7 64 101

answered Apr 15 '15 at 22:40


Jorge Zaldívar
61 1

Jorge, I've amended the code


formatting in your answer - for future
reference, you can highlight the code
block and press the {} button in the
text editor to do this for you. – Sam
Apr 15 '15 at 22:45

Probably the simplest logic to follow. –


brettdj Apr 16 '15 at 0:44

You could evaluate an expression


with a worksheet formula:
6
not_use$ = "43,83,87,91,92,93,98"

For i = 1 To 99
If Application.Evaluate("ISERROR(
'// Do Something
End If
Next i

This means the "test" is evaluated in


one go rather than using multiple
criteria or further loops.

edited Apr 16 '15 at 6:57

answered Apr 15 '15 at 22:43


https://stackoverflow.com/questions/29661897/loop-through-all-numbers-in-a-range-except-a-few 3/6
4/11/2019 Samexcel - Loop through all numbers in a range except a few - Stack Overflow
15.7k 3 30 55

Clever approach. – brettdj Apr 16 '15


at 0:43

@brettdj cheers :) I'm a big believer in


"don't re-invent the wheel" so if there's
already a worksheet function to do the
job (and it's quicker), I'll try and
incorporate that somewhere. – Sam
Apr 16 '15 at 0:52

1 I think Not is redundant but


nonetheless deserves a UV even
though I'm not a fan of Evaluate
function. :) – L42 Apr 16 '15 at 1:05

@L42 you are absolutely right. Got


carried away there, I shall edit and
remove the oxymoron at once!
However I think the Evaluate
method is greatly underrated in VBA
and can often save a lot of time if
used effectively – Sam Apr 16 '15 at
1:10

I use it as well. But not if I can get


away with it. Based on experience,
result can be unpredictable especially
if you are to try to evaluate complex
formulas within a complex routine. –
L42 Apr 16 '15 at 1:11

@L42 I agree when it comes to


complex formulae, I would at that
point prefer to use a UDF or
something - even if it's just to avoid
having to build the expression with
extensive string concatenation! – Sam
Apr 16 '15 at 1:21

Smaller code perhaps. If this is


useful.
1
not_use = Split("41,83,87,91,92,93,98
For i = 1 To 99
If UBound(Filter(not_use,CStr(i))
Next

Just realized you were asking for


VBA and not VBScript. My bad.

answered Apr 15 '15 at 22:40


Pankaj Jaju
4,078 2 17 32

Filter is available in VBA. Only


problem is that it matches sub-strings
and skips 2, 4, and 7 for some reason.
– Comintern Apr 15 '15 at 23:08

https://stackoverflow.com/questions/29661897/loop-through-all-numbers-in-a-range-except-a-few 4/6
4/11/2019 excel - Loop through all numbers in a range except a few - Stack Overflow

This is a very crude example but with


solve your problem:
1
Sub ArrayLoopExample()

Dim MyArray As Variant: MyArray = Arr

For i = 1 To 99
For x = LBound(MyArray) To UBound

If i = MyArray(x) Then

'Skip

Else

'Some code

End If

Next x
Next i

End Sub

UPDATE as per comments below.

edited Apr 15 '15 at 23:04

answered Apr 15 '15 at 22:36


Matt D. Webb
2,276 4 21 38

Only just seen your update regarding


string variable, – Matt D. Webb Apr 15
'15 at 22:40

1 Matt you can shorten your code like


this: Dim MyArray as variant MyArray
= Array(43,83,87,91,92,93,98) –
Dan Donoghue Apr 15 '15 at 23:02

1 @DanDonoghue Genius! I've updated


my answer. – Matt D. Webb Apr 15 '15
at 23:04

FYI, It was only on the same line for


me because it was in a comment, it's
fine as it is but I prefer personally to
not use the : and have those two
commands on seperate lines.
Personal preference, makes no
difference to the code though :) –
Dan Donoghue Apr 15 '15 at 23:06

Just a variation on what above


answers already does
1
Dim not_use As Variant, i As Integer
not_use = Array(43, 83, 87, 91, 92, 9
https://stackoverflow.com/questions/29661897/loop-through-all-numbers-in-a-range-except-a-few 5/6
4/11/2019 For i = 1 To 99 excel - Loop through all numbers in a range except a few - Stack Overflow
If IsError(Application.Match(i, n
' do some cool stuff
End If
Next

answered Apr 16 '15 at 1:02


L42
16.8k 7 32 56

https://stackoverflow.com/questions/29661897/loop-through-all-numbers-in-a-range-except-a-few 6/6

Você também pode gostar