VBA code to add time stamp to a list of cells in a column

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I need to write some code which adds a time stamp that will not change to a list of cells in a column.

To give a sample example, when a button is clicked, the code would at a time stamp to the beginning of each of the cells below.

The data below covers 4 rows, with the numbers 1, 2, 3, and 4 in cells A1, A2, A3 and A4, respectively.

Has anyone done this before?

TIA

1
2
3
4

<colgroup><col width="64" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about:
Code:
Sub add_stamp()

Dim cl As Range
Dim rng As Range
Dim str As String
Dim val As String

Set rng = Me.Range("A1:A4")

str = Format(Time(), "HH:MM")

    For Each cl In rng
        val = cl.Value
        cl.Value = str & " " & val
    Next
End Sub
 
Upvote 0
Hi Sykes

Thanks for the prompt response.

When I try to run the code, however, I get a "Compile error" that says "Invalid use of Me keyword."

Do you know why that may be?

Also, the number of rows of data will vary. Do you know how to amend the code so that it adds a time stamp to every row in column A with data?

Thanks in advance.
 
Upvote 0
If you can be more specific when initially posting, it will save wasted time....

I've (lazily) used the "me" keyword to keep things short & curt.
For this reason, this code needs to be placed into the sheet module, of the worksheet in question - not just into any module.
Code:
Sub add_stamp()

Dim cl As Range
Dim rng As Range
Dim str As String
Dim val As String
Dim rw As Long

rw = Me.Range("A65536").End(xlUp).Row

Set rng = Me.Range("A1:A" & rw)

str = Format(Time(), "HH:MM")

    For Each cl In rng
        If cl.Value = "" Then GoTo Next_cl
        val = cl.Value
        cl.Value = str & " " & val
Next_cl:
    Next
End Sub

if you want it to work on any of your worksheets, then
Code:
Sub add_stamp()

Dim cl As Range
Dim rng As Range
Dim str As String
Dim val As String
Dim rw As Long

rw = ActiveSheet.Range("A65536").End(xlUp).Row

Set rng = ActiveSheet.Range("A1:A" & rw)

str = Format(Time(), "HH:MM")

    For Each cl In rng
        If cl.Value = "" Then GoTo Next_cl
        val = cl.Value
        cl.Value = str & " " & val
Next_cl:
    Next
End Sub
.....should do the trick.
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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