repeatinng a macro the number of times specified in a cell

snipescc

Board Regular
Joined
Aug 29, 2010
Messages
136
Office Version
  1. 365
Platform
  1. Windows
I have a macro that calculates a sheet, waits 1 second, then does it again. After 12 seconds, it switches to the next sheet and does the same thing. I'd like the macro to run certain number of times, based on the value of a cell that the end user inputs into a box. I've minimal experience with writting VBA, so I'd appreciate some help. Here is my code (without the repeats every second, I can fix that once I've gotten the looping working)

Sub TreeTwinkle()
'
' TreeTwinkle Macro
'
Dim myValue As Integer
myValue = InputBox("How many minutes should this run?")
Range("da1").Value = myValue


ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Menorah").Select
ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Cresant").Select
ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Snowflake").Select
ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Khanda").Select
ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Tree").Select
ActiveSheet.Calculate


End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I'm a rookie as well, but I think this will work.

Code:
Sub TreeTwinkle()
'
' TreeTwinkle Macro
'
Dim myValue As Integer
myValue = InputBox("How many minutes should this run?")


Do While Not IsEmpty(myValue)
If myValue = 0 Then GoTo jmp1
    myValue = myValue - 1


ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Menorah").Select
ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Cresant").Select
ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Snowflake").Select
ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Khanda").Select
ActiveSheet.Calculate
Application.Wait Now + #12:00:01 AM#
Sheets("Tree").Select
ActiveSheet.Calculate
Loop


jmp1:


End Sub

RobMac
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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