Formatting Date in VBA?

Matt_O

Board Regular
Joined
May 23, 2013
Messages
64
Hi Folks,

I haven't had any luck in getting my code to reformat Date from 5/14/2018 to something without the forward slash. The code I found when I searched for a solution simply doesn't work for some reason.

Here's my code showing all the iterations I've tried to reformat the Date.

Code:
    Dim aDate As Date
    aDate = Date  ' returns 5/14/2018
    
    ' version 1
    Dim myDate As String
    myDate.Value = format(aDate, "yyyy-mm-dd")

    ' version 2
    Dim myDate As String
    myDate = format(aDate, "yyyy-mm-dd") 

    ' version 3
    Dim myDate As Date
    myDate = format(Date, "yyyy-mm-dd")

    ' version 4
    Dim myDate As String
    myDate = format(Date, "yyyy-mm-dd")

The VBA editor always changes the F in Format to a lowercase and I get the error "Wrong number of arguments or invalid property assignment."

All around the web I kept finding this code
Code:
(variable) = Format(Date, "DD-MM-YYYY") ' or similar

I'm stumped again in VBA. Although I did figure out the difference between Now and Date!

Thanks in advance to anyone who replies.

Cheers,
Matt
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Matt

Have you named anything 'format'?
 
Upvote 0
When I tested the following, it worked for me.

Code:
Option Explicit


Sub fooDate()
    Dim aDate As Date, myDate As String
    aDate = Date  ' returns 5/14/2018
    
    ' version 1
    myDate = Format(aDate, "yyyy-mm-dd")
    MsgBox (myDate)


    ' version 2
    myDate = Format(aDate, "yyyy-mm-dd")
    MsgBox (myDate)
    
    ' version 3
    myDate = Format(Date, "yyyy-mm-dd")
    MsgBox (myDate)
    ' version 4
    
    myDate = Format(Date, "yyyy-mm-dd")
    MsgBox (myDate)
End Sub
 
Upvote 0
Thanks for the prompts replies Norie and Alan.

When I copied Alan's code the editor made all the Fs lowercase gain. It turns out I did have a sub called "format" that I didn't use and completely forgot about it!

Alan's code works but the editor refuses to maintain the uppercase F in format. Maybe a re-boot will fix this. At least the code works.

Thanks again, much appreciated!

Matt
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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