Need Macro to insert current date & time into selected c

MPBJR

Board Regular
Joined
Mar 28, 2007
Messages
143
I use a excel file through the course of the day and need to insert the current date in one column and the current time in the next column. I want to be able to just highlight the selected range of cells I need to insert into and hit a macro button and have the date and time inserted into just the cells I have highlighted. I'm not sure how to make this work with just the cells I've highlighted. Any help any one can give me would be greatly appreciated. Thanks!
Mike
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Insert Date

Hello,

Welcome to the Board!

Here is some code that should do it for you!


Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean,  Cancel As Boolean)
Range("A1") = Now        'Select any cell you want 
End Sub

HTH

Again welcome to the board!

Kurt
 
Upvote 0
You can also select the date cell and hit "Ctrl-;".
Then select the time cell and hit "Ctrl-Shift-:".
 
Upvote 0
You can also select the date cell and hit "Ctrl-;".
Then select the time cell and hit "Ctrl-Shift-:".

This is what I've been using but I would rather use a macro. I have a couple other steps I'm doing as well that I a;ready know how to do, but got stuck with at this point.
Thanks
 
Upvote 0
Code:
Sub GetDateTime()
    ActiveCell.Value = Date
    ActiveCell.Offset(, 1).Value = Time
End Sub
 
Upvote 0
Code:
Sub GetDateTime()
ActiveCell.Value = Date
ActiveCell.Offset(, 1).Value = Time
End Sub

This is getting close to what I want, now if I select let's say range of a1 to b3, I would want it to post date and time 3 times. Is this possible?
 
Upvote 0
Select A1:B1
Run the macro code.
Drag the fill handle in B1 down to B3 while holding Ctrl.
(Holding Ctrl key stops number incrementing during the copy.)
 
Upvote 0
What have you tried?

This seems to work.
Code:
Sub GetDateTime()
    Set Rng = Selection
    For Each c In Rng
        If c.Column = ActiveCell.Column Then
            c.Value = Date
            c.Offset(, 1).Value = Time
        End If
    Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,501
Messages
6,114,010
Members
448,543
Latest member
MartinLarkin

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