Solved

How do I format a date and time in Excel VBA ?

Posted on 2013-12-18
7
486 Views
Last Modified: 2013-12-19
Hi,

I have an Excel worksheet with date/time combinations held as 3 separate cells in the following formats:

Cell 1 (Date):             dd-mm-yyyy    (eg 24-Dec-2013)
Cell 2 (Time):             #n.nn     (eg 2.30)
Cell 3 (AM/PM):         xx      (eg  AM)

Using the data in these 3 cells I want to create a VBA function which will output a string in the following format:

[dddd] [dd] [mmmm] [yyyy] [hh.mm][am/pm]

For example,  Tuesday 24 December 2013 2.30pm

Any ideas ?

Thanks
Toco
0
Comment
Question by:Tocogroup
  • 3
  • 3
7 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39728561
Hi

Are the cells values dates/time or text?

Regards
0
 

Author Comment

by:Tocogroup
ID: 39728567
Hi

The cells values are formatted as follows:

Date: Date (dd-mm-yyyy)
Time: Number (to 2 decimal places)
AM/PM: General
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39728584
Hi

pls try

Function fFormatDateComplete(dt As Date, time As Double, AM_PM As String) As String
    fFormatDateComplete = Format(dt + TimeValue(Replace(Format(time, "0.00"), ".", ":") & " " & AM_PM), "dddd dd MMMM yyyy hh.mm am/pm")
End Function

Open in new window

Regards
0
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.

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39728641
Do you really need VBA for some specific reason rather than a regular formula?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39728645
Hi,

the formula would be

=TEXT(F4+TIMEVALUE(SUBSTITUTE(TEXT(G4;"0.00"),".",":")&" "&H4 ), "dddd dd MMMM yyyy hh.mm am/pm")

Open in new window

Regards
0
 

Author Comment

by:Tocogroup
ID: 39728677
Hi Rorya,

I'm using this expanded date and time value in a user form, and only once, so I don't want to create another column on my worksheet just for this one instance.
0
 

Author Closing Comment

by:Tocogroup
ID: 39728680
That's perfect. Thanks very much.
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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
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…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

863 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

20 Experts available now in Live!

Get 1:1 Help Now