Vba timestamp when pressing a button

johnny70

New Member
Joined
Dec 22, 2015
Messages
21
I have started to create a excel with vba that should put a timestamps in cells a row when pressed.
On same sheet I have a counter, calculation of "cycletime" and also a conditional formating to highlight if a cycle is larger that set value (also displayed in sheet)
1710426629841.png


This is the code used for Clickbutton:
Private Sub Click()


If koll = 1 Then
Worksheets("User Data").Protect Contents:=False
Worksheets("User Data").Cells(countunit, 1) = Format(Now, "mm/dd/yyyy HH:mm:ss")
countunit = countunit + 1
koll = 1

Cells(2, 4) = "Antal enheter"
Cells(3, 4) = countunit - 9

End If

Worksheets("User Data").Protect Contents:=True

End Sub


Initial question is how do I create a sub that make it possible to close and open the file and it continues from last position.
Current I need to use a reset Sub to make ut work but the I lose previous data
This is the code for reseting

Private Sub Reset_Click()
countunit = 9
koll = 1
Worksheets("User Data").Protect Contents:=False
Worksheets("Background Data").Protect Contents:=False

Worksheets("User Data").Cells(8, 1) = "Timestamp for unit to station"


Worksheets("User Data").Cells(3, 4) = 0
Worksheets("User Data").Range("A9:A800").ClearContents
Worksheets("User Data").Range("C9:C800").ClearContents

'Worksheets("Data").Cells.ClearContents

Worksheets("User Data").Cells(8, 1) = "Timestamp for unit to station"
Worksheets("User Data").Cells(2, 4) = "Number of units"
Worksheets("User Data").Cells(9, 1) = 0
Worksheets("Background Data").Cells(2, 6) = "Value for row calculation"
Worksheets("Background Data").Cells(3, 6) = countunit
Worksheets("Background Data").Cells(4, 6) = "Uppdated"
Worksheets("Background Data").Cells(4, 6) = Format(Now, "mm/dd/yyyy HH:mm:ss")


Worksheets("User Data").Protect Contents:=True
Worksheets("Background Data").Protect Contents:=True

End Sub
 

Attachments

  • 1710426571496.png
    1710426571496.png
    20.8 KB · Views: 7

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You don't say where and how the variables "koll" and "countunit" are declared, so I'll just make a guess presuming they are declared as Global. Try this macro of mine to be pasted into the "ThisWorkBook" module. Eventually you can use it as a suggestion and tweak the rest of the project.
VBA Code:
Option Explicit
Private Sub Workbook_Open()
    Dim lRow  As Long
    lRow = Sheets("User Data").Cells(Rows.Count, "A").End(xlUp).Row
    If lRow >= 8 Then
        koll = 1
        countunit = lRow + 1
    End If
End Sub
 
Upvote 0
Solution
You don't say where and how the variables "koll" and "countunit" are declared, so I'll just make a guess presuming they are declared as Global. Try this macro of mine to be pasted into the "ThisWorkBook" module. Eventually you can use it as a suggestion and tweak the rest of the project.
VBA Code:
Option Explicit
Private Sub Workbook_Open()
    Dim lRow  As Long
    lRow = Sheets("User Data").Cells(Rows.Count, "A").End(xlUp).Row
    If lRow >= 8 Then
        koll = 1
        countunit = lRow + 1
    End If
End Sub
This is the initial variable declaration
Public countunit As Integer
Public koll As Integer


I did a copy of first the first code that used koll as a variable, maby I could do without
1710442573501.png
 
Upvote 0
Tnx.
Placed your code in my click sub: - It works (y)

Public countunit As Integer

Public koll As Integer


Private Sub Click()

Dim lRow As Long
lRow = Sheets("User Data").Cells(Rows.Count, "A").End(xlUp).Row
If lRow >= 8 Then
koll = 1
countunit = lRow + 1
End If

If koll = 1 Then
Worksheets("User Data").Protect Contents:=False
Worksheets("User Data").Cells(countunit, 1) = Format(Now, "mm/dd/yyyy HH:mm:ss")
countunit = countunit + 1
koll = 1

Cells(2, 4) = "Antal enheter"
Cells(3, 4) = countunit - 9

End If

Worksheets("User Data").Protect Contents:=True

End Sub
 
Upvote 0
So I see that you worked it out(y), glad having been of some help :biggrin:.
By the way, you probably need to mark this thread as [Solved].
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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