VBA enter time into a cell in format HH:MM am/pm

Oliver Dewar

Board Regular
Joined
Apr 17, 2011
Messages
201
Hi All,

I need to enter the current time into a cell using VBA in this format:

HH:MM am/pm - so no seconds.

eg

10:10 am

At the moment I'm using:

Code:
range("A1").value = Time

and am getting the annoyingly precise seconds in the cell as well.

This time will be added to a string of text in the cell so I cannot simply format the cell.

Any help you can give me will be greatly appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
try this
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Range("A1")
.Value = Time
.NumberFormat = "h:mm AM/PM"
End With
End Sub
 
Upvote 0
Thanks Grizz... that's helping me to get my thoughts straight.

You know what I think I really need to do?

Create a function called Time2 that puts the time in, in the format described above.

I've never written a function before, how would I do it?
 
Upvote 0
Thanks Grizz,

I'm going to put my question in as a new post, as my question has now changed so much.

Regards,

Oliver
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,219
Members
452,895
Latest member
BILLING GUY

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