Solved

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

Posted on 2013-12-18
7
475 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 48

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 48

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
Highfive Gives IT Their Time Back

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!

 
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 48

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

707 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

18 Experts available now in Live!

Get 1:1 Help Now