2

I need to loop through i = 1 to 99 but I want to skip a few specific i values. 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.

4
  • What have you tried? Please can you provide some code you have attempted yourself. Commented Apr 15, 2015 at 22:18
  • added code I have tried. Commented Apr 15, 2015 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. Commented Apr 15, 2015 at 23:08
  • So many good answers!!! Thanks 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. Commented Apr 15, 2015 at 23:12

6 Answers 6

6

You can specify the values to ignore in a more compact way than with your 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
2
  • 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. Commented Apr 15, 2015 at 22:45
  • Probably the simplest logic to follow.
    – brettdj
    Commented Apr 16, 2015 at 0:44
6

You could evaluate an expression with a worksheet formula:

not_use$ = "43,83,87,91,92,93,98"

For i = 1 To 99
    If Application.Evaluate("ISERROR(MATCH(" & i & ",{" & not_use & "},0))") Then
        '// Do Something
    End If
Next i

This means the "test" is evaluated in one go rather than using multiple criteria or further loops.

5
  • @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. Commented Apr 16, 2015 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
    Commented Apr 16, 2015 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 Commented Apr 16, 2015 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
    Commented Apr 16, 2015 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! Commented Apr 16, 2015 at 1:21
1

Throw i substituted with nothing at the string length and compare:

Sub LoopSkip()
     Dim NotUse As String
     NotUse = "41,83,87,91,92,93,98"
     For i = 1 To 99
        If Len("," & NotUse & ",") = Len(Replace("," & NotUse & ",", "," & i & ",", "")) Then
             'Do Stuff
         End If
     Next i
End Sub
4
  • 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 ? Commented Apr 15, 2015 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 :). Commented Apr 15, 2015 at 22:52
  • If it "doesn't" find the number the string length is the same which allows it to enter the routine. Commented Apr 15, 2015 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. :) Commented Apr 17, 2015 at 19:46
1

Smaller code perhaps. If this is useful.

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

Just realized you were asking for VBA and not VBScript. My bad.

1
  • Filter is available in VBA. Only problem is that it matches sub-strings and skips 2, 4, and 7 for some reason.
    – Comintern
    Commented Apr 15, 2015 at 23:08
1

This is a very crude example but with solve your problem:

Sub ArrayLoopExample()

Dim MyArray As Variant: MyArray = Array(43, 83, 87, 91, 92, 93, 98)

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

        If i = MyArray(x) Then

        'Skip

        Else

        'Some code

        End If

    Next x
Next i

End Sub

UPDATE as per comments below.

3
  • Only just seen your update regarding string variable, Commented Apr 15, 2015 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) Commented Apr 15, 2015 at 23:02
  • 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 :) Commented Apr 15, 2015 at 23:06
1

Just a variation on what above answers already does

Dim not_use As Variant, i As Integer
not_use = Array(43, 83, 87, 91, 92, 93, 98) ' create an array

For i = 1 To 99
    If IsError(Application.Match(i, not_use, 0)) Then
        ' do some cool stuff
    End If
Next

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.