Toggle Button to Start and Stop Macros

kwooden

New Member
Joined
May 5, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have a user form with a toggle button which calls two different macros when the button is pressed. I'm trying to stop running both macros when the button is depressed but not sure of the correct command. I've been searching all over the internet without any luck. I'm sure it's something very simple Below is the code I currently have.

Private Sub ToggleButton1_Click()
If ToggleButton1 = True Then
ToggleButton1.Caption = "Start"
Call Calculate_Range
Call DisplayTime
Else
ToggleButton1.Caption = "Stop"
End If
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
is this what you want??
VBA Code:
Private Sub ToggleButton1_Click()
If ToggleButton1 = True Then
ToggleButton1.Caption = "Start"
Call CalCulate_range
Else
ToggleButton1.Caption = "Stop"
Call DisplayTime
End If
End Sub
 
Upvote 0
Offthelip,

Thanks for your reply. I actually want the toggle button to start running the two macros (Calculate_Rang & DisplayTime). This part already works, but what I'm trying to do is end both macros when the button is pressed again. Right now if I close the form both macros continue to run in the background.
 
Upvote 0
You will have to put some code into the two macros. I would do this by declaring a public variable which is set TRUE by your toggle button code when you call the two subroutines. When you press the button again set the flag to FALSE, then in the two subroutines you continually test this flag and is it is FALSE EXIT sub, You can clear the flag in other ways e.g when the form is closed without pressing the button again
 
Upvote 0
Offthelip,

I'm still a novice when it comes to all of this. I'm not sure how to do this but below are the two macros that I'm using.

Sub Calculate_Range()
Range("A1:A5").Calculate
Application.OnTime DateAdd("s", 1, Now), "Calculate_Range"
Application.EnableEvents = True
End Sub


***************
Public nTime As Double
Sub DisplayTime()

UserForm1.tbCurrDt = Format(Now(), "dddd, mmmm dd, yyyy hh:mm:ss")
nTime = Now() + TimeSerial(0, 0, 1)
Application.OnTime nTime, "DisplayTime"
End Sub
 
Upvote 0
In the toggle button code:
VBA Code:
Private Sub ToggleButton1_Click()
If ToggleButton1 = True Then
ToggleButton1.Caption = "Start"
Activeflag = True
Call Calculate_Range
Call DisplayTime
Else
Activeflag = False
ToggleButton1.Caption = "Stop"
End If
End Sub
Then in a separate module:
VBA Code:
Public nTime As Double
Public Activeflag As Boolean

Sub Calculate_Range()
Range("A1:A5").Calculate
If Activeflag Then
Application.OnTime DateAdd("s", 1, Now), "Calculate_Range"
End If
Application.EnableEvents = True
End Sub


Sub DisplayTime()
UserForm1.tbCurrDt = Format(Now(), "dddd, mmmm dd, yyyy hh:mm:ss")
If Activeflag Then
nTime = Now() + TimeSerial(0, 0, 1)
Application.OnTime nTime, "DisplayTime"
Cells(1, 2) = Cells(1, 2) + 1
End If
End Sub
 
Upvote 0
In the toggle button code:
VBA Code:
Private Sub ToggleButton1_Click()
If ToggleButton1 = True Then
ToggleButton1.Caption = "Start"
Activeflag = True
Call Calculate_Range
Call DisplayTime
Else
Activeflag = False
ToggleButton1.Caption = "Stop"
End If
End Sub
Then in a separate module:
VBA Code:
Public nTime As Double
Public Activeflag As Boolean

Sub Calculate_Range()
Range("A1:A5").Calculate
If Activeflag Then
Application.OnTime DateAdd("s", 1, Now), "Calculate_Range"
End If
Application.EnableEvents = True
End Sub


Sub DisplayTime()
UserForm1.tbCurrDt = Format(Now(), "dddd, mmmm dd, yyyy hh:mm:ss")
If Activeflag Then
nTime = Now() + TimeSerial(0, 0, 1)
Application.OnTime nTime, "DisplayTime"
Cells(1, 2) = Cells(1, 2) + 1
End If
End Sub
Offthelip,

This works perfectly!! Thank you so much.
 
Upvote 0
Offthelip,

If I may ask one last question. I have 3 text box fields with dates (today & end date) and the 3rd box is the difference between the two dates that is formatted as months, days, hours, minutes, and second. I my Sub "DisplayTime" makes the "Today" textbox show a running digital time. However, I have not been able to manipulate the formula to apply to my 3rd textbox to show the time counting down as it gets closer to the end date. Can you assist with this?
 
Upvote 0
I would try something like this:
VBA Code:
Sub DisplayTime()
UserForm1.tbCurrDt = Format(Now(), "dddd, mmmm dd, yyyy hh:mm:ss")
UserForm1.tbEndDt = Format((EnddateV - Now()), "dddd, mmmm dd, yyyy hh:mm:ss")    ' assuming variable enddateV is set and the text box is called enddate 
If Activeflag Then
nTime = Now() + TimeSerial(0, 0, 1)
Application.OnTime nTime, "DisplayTime"
'Cells(1, 2) = Cells(1, 2) + 1 Sorry I left this in by mistake it was part of my testing code
End If
End Sub
 
Upvote 0
The textboxes are as follows:

tbCurrDt: set as Format(Now(), "dddd, mmmm dd, yyyy hh:mm:ss")
tbEndDt: a static date of 12 Sep 23 00:00:00 (Format(TextBoxValue, "dddd, mmmm dd, yyyy HH:MM:SS AM/PM")
tbTimetogo: if the calculated date difference set as Format(TextBoxValue, " m dd hh mm ss") which give me the months, days, hours, minutes, and seconds from current date to 12 Sep 23
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,147
Members
449,098
Latest member
Doanvanhieu

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