Need Macro to insert current date & time into selected c

MPBJR

Board Regular
Joined
Mar 28, 2007
Messages
138
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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Kurt

Well-known Member
Joined
Jul 23, 2002
Messages
1,664
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
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
You can also select the date cell and hit "Ctrl-;".
Then select the time cell and hit "Ctrl-Shift-:".
 

MPBJR

Board Regular
Joined
Mar 28, 2007
Messages
138
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
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985

ADVERTISEMENT

Code:
Sub GetDateTime()
    ActiveCell.Value = Date
    ActiveCell.Offset(, 1).Value = Time
End Sub
 

MPBJR

Board Regular
Joined
Mar 28, 2007
Messages
138
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?
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985

ADVERTISEMENT

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.)
 

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
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
 
Learn Excel from Bill Jelen

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

Forum statistics

Threads
1,151,607
Messages
5,765,396
Members
425,281
Latest member
tmoreira001

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
Top