Solved

Excel vba question

Posted on 2011-02-23
4
299 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

14 Experts available now in Live!

Get 1:1 Help Now