VBA Form - Need Button - Time - Start & pause

Sunil7598

New Member
Joined
Dec 28, 2022
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I have vba userform you can find ss below. When a user clicks on pause it changes to resume but its not changing to pause when clicking resume. And I want calculate time between those clicks
1680699167547.png

Here is a code that I used. This code is in command button
Private Sub CommandButton3_Click()
Static startTime As Date
Static pauseTime As Date
Static isPaused As Boolean


If Not isPaused Then
' Start the timer
startTime = Now()
CommandButton3.Caption = "Resume"
isPaused = False
Else

' Pause the timer
pauseTime = Now()
CommandButton3.Caption = "Pause"
isPaused = True
End If

End Sub


This code is in form
Private Sub UserForm1_Activate()
' Initialize the timer
startTime = Now()
pauseTime = startTime
End Sub


Dim elapsedTime As Date
If isPaused Then
elapsedTime = pauseTime - startTime
Else
elapsedTime = Now() - startTime
End If
ThisWorkbook.Sheets("Form").Range("G2").Value = Format(elapsedTime, "hh:mm:ss")


Please help me in solving this. Am trying to get it from 4 days
Thanks in Advance
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
your code
VBA Code:
If Not isPaused Then
should be
VBA Code:
If isPaused Then

Using NOT makes isPaused false
so when the button is = to pause isPaused =True
 
Upvote 0
Hi,

Just an observation,

your code shows that you have renamed the UserForm Activate event to UserForm1_Activate and in doing so, will not work as required.
It must be UserForm_Activate regardless of your userform name.

Never change this or any other events name

Dave
 
Upvote 0
your code
VBA Code:
If Not isPaused Then
should be
VBA Code:
If isPaused Then

Using NOT makes isPaused false
so when the button is = to pause isPaused =True
this is not working. Pause button is not changing to resume
 
Upvote 0
Hi,

Just an observation,

your code shows that you have renamed the UserForm Activate event to UserForm1_Activate and in doing so, will not work as required.
It must be UserForm_Activate regardless of your userform name.

Never change this or any other events name

Dave
if I change it to UserForm_activate then form is not opening
 
Upvote 0
if I change it to UserForm_activate then form is not opening

that shows event is now working & suggests you have a problem with your project

I suspect issue may be that these lines of code are outside a procedure?

VBA Code:
Dim elapsedTime As Date
If isPaused Then
elapsedTime = pauseTime - startTime
Else
elapsedTime = Now() - startTime
End If
ThisWorkbook.Sheets("Form").Range("G2").Value = Format(elapsedTime, "hh:mm:ss")

BTW - when I stated, "Never change this or any other events name" omitted that this relates to UserForm events.

Dave
 
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