VBA to set a custom date (time zone) format

well2ant

New Member
Joined
Dec 16, 2014
Messages
12
Hello all,

I'm writing a module to convert date & time provided in UTC format, into a local time zone of my choice.
The code works fine in converting to the required time zone, but I want to them place a formatting over the top to indicate which time zone it relates to.

I do not want to add text to the field, hence why I'm using a format.

If I select the cell(s) I can manually apply a custom number format of
"dd/mm/yyyy hh:mm:ss \(\A\E\D\T \U\T\C\+\1\1\)"
which makes the date/time appear as
21/06/2016 21:16:03 (AEDT UTC+11)

However, I'm trying to get the macro to apply the appropriate format of the time zone that has been calculated. I am using the code below, but it does not change the value in MyRange.NumberFormat from what it was before the macro is run.

Code:
        Select Case OutPut
          Case AEDT_Format
            MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\E\D\T \U\T\C\+\1\1\)"
          Case AEST_Format
            MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\E\S\T \U\T\C\+\1\0\)"
          Case ACDT_Format
            MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\C\D\T \U\T\C\+\1\0\.\5\)"
          Case ACST_Format
            MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\C\S\T \U\T\C\+\9\.\5\)"
          Case AWST_Format
            MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\W\S\T \U\T\C\+\8\)"
          Case Else
            MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss"
        End Select
      End With

OutPut: is an integer 0 to 4.

I realise the I do not need all the \ characters, but it ensures that each character is literal.

Does anyone have any suggestions?

Thank you in advance.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
If this works I would look at the variables like AEDT_Format to be sure they are set proper

Code:
Select Case OutPut
  Case 0
    MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\E\D\T \U\T\C\+\1\1\)"
  Case 1
    MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\E\S\T \U\T\C\+\1\0\)"
  Case 2
    MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\C\D\T \U\T\C\+\1\0\.\5\)"
  Case 3
    MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\C\S\T \U\T\C\+\9\.\5\)"
  Case 4
    MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss \(\A\W\S\T \U\T\C\+\8\)"
  Case Else
    MyRange.NumberFormat = "dd/mm/yyyy hh:mm:ss"
End Select
 
Upvote 0
Hi Warship,

The values are set correctly. I did have another thread out there - someone told me that you can't assigned the NumberFormat within a UDF, which is what I was trying to do.
I tried moving the code to a procedure and have the UDF call the procedure, but that didn't work either.

I'll have to work out another way of doing this.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top