Jump to content

vba macro for loop question


Recommended Posts

Hi i know this is unrelated to autoit, but i think most guys here are very good programmers and i hope a few might know this

so i have this for loop say from 1 to 500

in this loop, i'm going to do an if check, and if the condition turns out to be true in want it to go to the next one,

For b = 4 To AllSize
    'if we encounter Percentage Breakdown, which is really all hubs, so skip it
    If Range("D" & CStr(b)).Value = "Percentage Breakdown" Then
    'move on to b+1 
             Next b
    End If
       
 
    For c = 1 To SizeOfOrganized
        
        If Range("J" & CStr(SizeOfOrganized)).Value = HubName Then
        'this isn't an unique name, so we should break out of loop c and move on to b+1
            Next b
        Else
                   'i have a large script here, so it will do most of the work here
        End If
        
    Next c
    
Next b

here is my simplified code, i deleted most of the script, but u get the idea, i want it to be able to skip the rest of the loop and continue to the next count when the condition meets, but when i run this it says that i have to have a for statement for each next statement, how do i by pass this?

Edited by ToyBoi
Link to comment
Share on other sites

for the innerloop I would just use exitloop

but for the outer one .. (I'm assuming if its true you want to skip the inner loop)

I would create some kind of flag to tell you whether or not the inner loop should be hit.

hmm... I wonder which key is the any key :O

Link to comment
Share on other sites

when i use exit loop will it stop the loop all together and do the next line or will it move on to b+1, b/c that's what i need it to do, the loop should always run from start to finish

Edited by ToyBoi
Link to comment
Share on other sites

when i use exit loop will it stop the loop all together and do the next line or will it move on to c+1, b/c that's what i need it to do, the loop should always run from start to finish

if you use exitloop within the inner loop it will go the very next line following whatever loop it is in

for example

outerloop

code

innerloop

code

exitloop

innerloopend

<- should go here

outerloopend

For b = 4 To AllSize
    Dim flag As Boolean = false
    'if we encounter Percentage Breakdown, which is really all hubs, so skip it
    If Range("D" & CStr(b)).Value = "Percentage Breakdown" Then
    'move on to b+1 
             flag = true
             'Next b 
    End If
      
    If flag = false Then
    For c = 1 To SizeOfOrganized
        
        If Range("J" & CStr(SizeOfOrganized)).Value = HubName Then
        'this isn't an unique name, so we should break out of loop c and move on to b+1
            'Next b
            ExitLoop
        Else
                   'i have a large script here, so it will do most of the work here
        End If
        
    Next c
    End If
    
Next b
Edited by Aiakonai

hmm... I wonder which key is the any key :O

Link to comment
Share on other sites

thanks, but what about the first if statement how do i have that part to proceed to b+1 if it's true?, i can't just have it exit loo, b/c then it's still going to do a bunch of things that i don't want it to do

Edited by ToyBoi
Link to comment
Share on other sites

if you use exitloop within the inner loop it will go the very next line following whatever loop it is in

for example

outerloop

code

innerloop

code

exitloop

innerloopend

<- should go here

outerloopend

For b = 4 To AllSize
    Dim flag As Boolean = false
    'if we encounter Percentage Breakdown, which is really all hubs, so skip it
    If Range("D" & CStr(b)).Value = "Percentage Breakdown" Then
    'move on to b+1 
             flag = true
             'Next b 
    End If
      
    If flag = false Then
    For c = 1 To SizeOfOrganized
        
        If Range("J" & CStr(SizeOfOrganized)).Value = HubName Then
        'this isn't an unique name, so we should break out of loop c and move on to b+1
            'Next b
            ExitLoop
        Else
                   'i have a large script here, so it will do most of the work here
        End If
        
    Next c
    End If
    
Next b

thanks, this should fix my problem

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...