[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Excel vba question

Posted on 2011-02-23
4
Medium Priority
?
332 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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 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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

656 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