+ Reply to Thread
Results 1 to 3 of 3

Creating For Loop with 3 variables

Hybrid View

  1. #1
    Registered User
    Join Date
    12-26-2011
    Location
    San Francisco
    MS-Off Ver
    Excel 2010
    Posts
    45

    Creating For Loop with 3 variables

    I have 3 variables: a, b, c

    I want to throw together a simple For loop with as few lines as possible. I know I can dim an array and fill it with these values and then iterate through the array, but that seems like overkill for what I want to do. Here is what works:
    Dim arrTest(1 to 3)
    arrTest(1) = a
    arrTest(2) = b
    arrTest(3) = c
    
    For i = lbound(arrTest) to ubound(arrTest)
       'Do something
    Next i
    This is what I want to do (but I'm getting a type mismatch error):
    dim tracking as variant
    
    For Each tracking in [a,b,c]
       'Do something
    Next tracking
    Any idea if what I am trying to do is even possible?

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Creating For Loop with 3 variables

    For Each tracking in Array(a, b, c)
    ... but it's very inefficient to use a for-each loop with an array of scalars (versus a collection of objects)
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    12-26-2011
    Location
    San Francisco
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Creating For Loop with 3 variables

    I realize that from the previous posts you had written but I felt it would be an alright compromise for readability and I literally have only 3 variables to step through.

    Thanks for the syntax

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1