+ Reply to Thread
Results 1 to 11 of 11

Can't use array in nested for loop

  1. #1
    Registered User
    Join Date
    06-15-2006
    Posts
    9

    Can't use array in nested for loop

    The following vba code can't be executed in Excel vba. Why?

    Please Login or Register  to view this content.
    Last edited by tommycho; 05-26-2015 at 10:20 AM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Can't use array in nested for loop

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    The following vba code can't be executed in Excel vba. Why?
    Why do you think it should work?

  3. #3
    Registered User
    Join Date
    06-15-2006
    Posts
    9

    Re: Can't use array in nested for loop

    p(1) and p(2) are two independent variables like i and j, i don't understand why it doesn't work,

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Can't use array in nested for loop

    Edit your first post and wrap the code with the code tag.

    It's a MUST forum rule.

  5. #5
    Registered User
    Join Date
    06-15-2006
    Posts
    9

    Re: Can't use array in nested for loop

    First post edited. Can anyone help?

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Can't use array in nested for loop

    Please Login or Register  to view this content.
    If you understand above line of your code, you should realize why it will not work.

    Your code doesn't make sense anyway.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Can't use array in nested for loop

    This declares p(2) as a single (array) variable,
    Please Login or Register  to view this content.
    if you try that code you'll get a For control variable in use compile error - p(1) and p(2) are elements of the same variable.
    Last edited by Norie; 05-26-2015 at 10:30 AM.
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    06-15-2006
    Posts
    9

    Re: Can't use array in nested for loop

    Thanks Norie for the explanation. So p(1) and p(2) aren't independent variables but are elements of the same variable. Am my understanding correct?

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

    Re: Can't use array in nested for loop

    Your code is fine but for the fact that VBA doesn't allow the use of array elements as the control variables in a for/next loop.

    Dim p(2) is equivalent to Dim p(0 to 2) (or Dim p(1 to 2) if you use Option Base 1)
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Can't use array in nested for loop

    shg

    You can use an array element as the loop control variable - try the OP's code without the inner loop.

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

    Re: Can't use array in nested for loop

    Sorry, I meant you can't use them in nested loops. I learned that by trying a couple of years ago -- it would have been convenient.

    It seems like an odd restriction.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] For Loop nested in For Each Loop - Use For Each's [item] as row reference
    By Schluensen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-28-2014, 11:37 PM
  2. [SOLVED] Find function in nested loop breaking down - not on first time through loop
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-14-2014, 09:32 AM
  3. Find function in nested loop breaking down - not on first time through loop
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2014, 04:59 PM
  4. Next without For error in nested loop - Escaping a Nested Loop?
    By BeneRich in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-10-2012, 06:38 PM
  5. For each loop nested; getting stuck on one cell in first iteration of nested loop
    By Excel_vba in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-22-2009, 11:54 AM

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