Time Stamp

still learning

Well-known Member
Joined
Jan 15, 2010
Messages
826
Office Version
  1. 365
Platform
  1. Windows
Hi
I'm using the following code to put the time in a cell:
HTML:
Sub time()
    ActiveCell.Select
    ActiveCell.FormulaR1C1 = "=now()"
    Selection.NumberFormat = "h:mm AM/PM"
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
End Sub
Even though I have the NumberFormat as "h:mm AM/PM" I still get the date and seconds in the cell as well.

How do I chage the code to only get the Hour and Minutes?

Thanks


Mike
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try:
Code:
sub Time()
ActiveCell.Value = Time
end sub
 
Upvote 0
Hi JoeMo
I copied and pasted the code as is, but when I try to use it, I get a message box that says
"Compile Error"
"Expected Function or Variable"
Being that I'm still learning, did I miss something :confused:

Mike :)
 
Upvote 0
Where did you paste it? Do you have a worksheet active with at least one cell selected when you run the code? There's no way I can see why this simple 3-line module would generate a compile error.
 
Upvote 0
There's no way I can see why this simple 3-line module would generate a compile error.
The issue is that you used the vba keyword Time for the name of your sub as well as using it in your sub.

My suggestion is

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Time_stamp()<br>    <SPAN style="color:#00007F">With</SPAN> ActiveCell<br>        .NumberFormat = "h:mm AM/PM"<br>        .Value = Time<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Last edited:
Upvote 0
Hi, JoeMO, Pete
Well, I don't know what happined, but both seem to work fine. The code time() showes seconds also, which I can easily format out. The code Time_stamp() doesn't.

I figure since it's working, don't try and fix it :biggrin:

Thanks agian for your time

Mike :beerchug: :beerchug:

No, I havn't had any, YET :biggrin:
 
Upvote 0
Glad you have got something working and that the 'Reply' problem you were having seems to be resolved.

If you haven't done so already, you really should rename your macro so that it is not using a keyword as its name - that is not a good idea.
 
Upvote 0

Forum statistics

Threads
1,224,579
Messages
6,179,656
Members
452,934
Latest member
mm1t1

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