Using a goto statement inside a loop

tyler2000

New Member
Joined
Jan 22, 2018
Messages
4
Hi,

I need to do something on a series of accounts with a macro and I am stuck on how to use a goto statement inside a loop.

for example my codes is:

PHP:
Excel.Range("A2").select

accountno = excel.activecell.offset(0,0)

count = 0

do until XXXX

     if conditionA then goto nextaccount

     if conditionB then
         modify something with the account here
     end if

nextaccount:
     Excel.Activecell.offset(1,0).select
     count = count + 1 

loop

what i am trying to do here is if condition A is met, go directly to the next account and loop the whole process again. but if condition B is met, modify the account then DO NOT run the code "nextaccount" and loop the whole process again.

the weird thing is that, the exact same code works for me most of the time but sometimes it does not. why?

when i use one or two line only, it works most of the time, but when i change it to multiple lines as followed, it does not seems to work as i wanted

if use this "nextfunction" instead of "nextaccount"

PHP:
nextfunction:
    do something here (as simple as sendkey "K")
    do something here as well (as simple as sendkey "Enter")
    also do something (as simple as sendkey "Enter")
    count = count + 1


can someone please help? any help is appreciated!

thanks!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Re: [HELP NEEDED] using a goto statement inside a loop

Don't see why you need Goto.
Code:
Excel.Range("A2").select

accountno = excel.activecell.offset(0,0)

count = 0

do until XXXX

     if Not  conditionA then

         if conditionB then
             modify something with the account here
         end if

    end if

     Excel.Activecell.offset(1,0).select

     count = count + 1 

loop
 
Upvote 0
Re: [HELP NEEDED] using a goto statement inside a loop

Don't see why you need Goto.
Code:
Excel.Range("A2").select

accountno = excel.activecell.offset(0,0)

count = 0

do until XXXX

     if Not  conditionA then

         if conditionB then
             modify something with the account here
         end if

    end if

     Excel.Activecell.offset(1,0).select

     count = count + 1 

loop

hi Norie,

thank you for the help.

unfortunately i do need the goto statement as this is just a simplified code. the actual code has if within if and nested loop, therefore at some point i need it to get the next account and loop again
 
Upvote 0
Re: [HELP NEEDED] using a goto statement inside a loop

You still shouldn't need a Goto to move onto the next account, all you need to do is make sure your If/End Ifs etc are structured properly.
 
Upvote 0
Re: [HELP NEEDED] using a goto statement inside a loop

yea, i just realized where went wrong. thank you again for your help!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top