Inserting a Static Date and Time thru a Macro

C

C.Pirabu

Hi!

I have to insert a Static Date and Time in a Cell thru a Macro.

I know the key strokes for this function.

Current date Select a cell and press CTRL+;
Current time Select a cell and press CTRL+SHIFT+;

But I need to accomplish this in a Macro. Could anyone help me with
this?

When I tried to record a macro to capture these key strokes it didn't
work.
 
O

okrob

That's because the macro recorder won't pickup the control button...
Try this:

Sub EnterDate()
Dim thisdate As String
thisdate = Date$
Sheets(Sheet1).Range("B4").Value = thisdate
' or you could use the sheets vba name
' Sheet1.Range("B4").Value = thisdate

End Sub

Replace the sheet and range with your own and run the macro

Rob
 
O

okrob

Sorry, you wanted both time and date:

Sub EnterDate()
Dim thisdate As String, thistime As String
thisdate = Date$
thistime = Time$
Sheets("Sheet1").Range("B4").Value = thisdate
' or you could use the sheets vba name
'Sheet1.Range("B4").Value = thisdate
Sheets("Sheet1").Range("B5").Value = thistime
End Sub

Also on the previous, I forgot the "" around the sheet name
Sheets(Sheet1) should be Sheet("Sheet1")

Rob
 
C

C.Pirabu

Great. Worked like a charm. Thanks a lot.
Sorry, you wanted both time and date:

Sub EnterDate()
Dim thisdate As String, thistime As String
thisdate = Date$
thistime = Time$
Sheets("Sheet1").Range("B4").Value = thisdate
' or you could use the sheets vba name
'Sheet1.Range("B4").Value = thisdate
Sheets("Sheet1").Range("B5").Value = thistime
End Sub

Also on the previous, I forgot the "" around the sheet name
Sheets(Sheet1) should be Sheet("Sheet1")

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top