?
Solved

Excel vba question

Posted on 2011-02-23
4
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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