Sign in to follow this  
Followers 0
ToyBoi

vba macro for loop question

8 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

thanks, this should fix my problem

yay I actually helped someone ... even if it wasn't an autoit thing.


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

Share this post


Link to post
Share on other sites

yay I actually helped someone ... even if it wasn't an autoit thing.

lol u are a smart guy, u can help many more ppl in the future

Share this post


Link to post
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
Sign in to follow this  
Followers 0