Solved

Excel vba question

Posted on 2011-02-23
4
295 Views
Last Modified: 2012-05-11
Hi, my excel code returns the time in military time - my OS is not set to military time, how can I take the time function out of returning military time?

I am using Excel 2010


Thanks much,



Dim x As Date

x = Format(Date, "mm-dd-yyyy")


Selection = x & "---" & Time

0
Comment
Question by:rinkydink
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34966829
You can force AM:PM as below

Regards

dave
Dim x As Date

Selection = Format(Date, "mm-dd-yyyy") & "--" & Format(Time, "hh:mm:ss AMPM")

Open in new window

0
 
LVL 1

Author Comment

by:rinkydink
ID: 34967765
Yes, I tried using Format(Time, "hh:mm:ss ampm") but it still returns the time in military time format.

I have the same OS Regional settings on my pc at home and Selection=Time returns the time in hh:mm:ss AMPM but I run Office 2007 at home and 2010 at work but I can find no setting in 2010 that appears to be forcing military time.

0
 
LVL 4

Accepted Solution

by:
rowanscott earned 500 total points
ID: 34968390
Hi Rinkydink

If you declare x as a date that is a value and is not concerned with the visible format.

It depends more on where you are displaying the date.

if its a worksheet cell i would tend to do as per code below.

Hope this helps


Option Explicit

Sub Macro1()
Dim r As Range, x As Date

x = Now
Set r = Sheet1.Range("A1")
r.Value = x
r.NumberFormat = "h:mm AM/PM"
End Sub


Sub Macro2()
Dim r As Range, x As Date

'better way to input times and dates. gives more control
x = DateSerial(Year(Date), Month(Date), Day(Date)) + TimeSerial(Hour(Now), Minute(Now), 0)

Set r = Sheet1.Range("A2")
r.Value = x
r.NumberFormat = "d mmm yyyy h:mm AM/PM"
End Sub


'if its in a vba form and you want to output a readable string
'use the value as follows

Dim strDate as String
Dim x as Date

x= Now
strDate=Format(x,"d mmm yyyy h:mm AM/PM")

Open in new window

0
 
LVL 4

Expert Comment

by:rowanscott
ID: 34968413
Oh i should have mentioned,
Macro1 and Macro2 are the same except for using Dateserial and Timeserial
I just wanted to point out the value of those functions because it makes it easy to leave out the seconds if you don't actually want them. It also makes it easy to get dates such as last date of previous month, Last quarter, and things like that.

Best Regards
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show you how to use shortcut menus in the Access run-time environment.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now