Making a code loop indefinitely

Bonaocity729

New Member
Joined
Feb 23, 2018
Messages
5
Hi everyone,
I'm new at this. I copied the below code from one of the thread. It works perfectly but I want to make it loop until I decide to cancel it. Thanks in advance.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("FEB-Total_pcs_per_hr")
Set sh2 = Sheets("Daily_Info")
If Sh.Name = sh1.Name Then
Application.Wait Time + TimeSerial(0, 0, 10)
sh2.Activate
End If
If Sh.Name = sh2.Name Then
Application.Wait Time + TimeSerial(0, 0, 15)
sh1.Activate
End If
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Re: help making a code loop indefinitely

I set up a workbook with your sheet names, put your code in the workbook module, and it's been running for about 10 minutes now. Does it eventually stop itself at some point? Or can you not stop it?

Just went back to it (still about 10 or 11 minutes of runtime), and it stopped on ESC.

I guess maybe I'm misunderstanding what you want, or how it's not doing what you want it to do.
 
Last edited:
Upvote 0
Re: help making a code loop indefinitely

Hi everyone,
I'm new at this. I copied the below code from one of the thread. It works perfectly but I want to make it loop until I decide to cancel it. Thanks in advance.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Sheets("FEB-Total_pcs_per_hr")
Set sh2 = Sheets("Daily_Info")
If Sh.Name = sh1.Name Then
Application.Wait Time + TimeSerial(0, 0, 10)
sh2.Activate
End If
If Sh.Name = sh2.Name Then
Application.Wait Time + TimeSerial(0, 0, 15)
sh1.Activate
End If
End Sub




jproffer

Thanks for your response. It usually run for about 10 minutes then it stops on it's own.
 
Upvote 0
Re: help making a code loop indefinitely

I can't imagine why it would stop. I'll have to wait until I get back to work to test it longer, but just looking at the code, I don't see anything that would stop it.
 
Upvote 0
Re: help making a code loop indefinitely

Ok. It's running right now...about 15 minutes now.

Is there any other code in in your workbook module? If so, could you post it?

Any of the worksheet modules? Or even regular modules.

It would have to be some other coding somewhere stopping it at the 10 minute mark.
 
Upvote 0
Re: help making a code loop indefinitely

Id did finally stop itself.

Ya got me friend...I still can't see why, but it lasted about 45 minutes total. As soon as I clicked the other tab, it triggered the event again, but I'm at a loss why it stopped in the first place. Hopefully someone smarter than I can come along and explain it to both of us.
 
Upvote 0
Re: help making a code loop indefinitely

Hi there
how do you write the vba code for a loop that will stop until a key is pressed (any key)

in foxbase coding, it goes somewhat like this

i=0
do while .not. keypressed
i=i+1
if keypressed
exit
endif
enddo
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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