Problem with the format of dates in VBA when declaring "As Date"

SeanE

New Member
Joined
Jul 10, 2011
Messages
7
Good evening all.

I was initially going to post a question about an issue with formatting dates in VBA, when adding 7 days onto the Date, but I've kind of solved the problem myself, which led to something else.

Code:
DIM afDate As Date
afDate = Format(Date + 7, "mm/dd/yyyy")
The above code would not work for me. No matter what I tried, afDate would always seem to return the date in the dd/mm/yyyy format, which is what my system is set to.

Whilst writing the post, though, I discovered that removing "DIM afDate As Date" rectified the problem.

My question now is why? Why will the code not word if I Dim afDate As Date? I would like to use Option Explicit, which means I need to declare afDate, though if I guess I'll have to leave it out if necessary.

Using Excel 2003 & Vista

Thank you for your time.

Sean
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Sean

afDate isn't really being formatted.

When you output afDate it's output in the system date format because it is a date.

The use of Format isn't really doing anything, it will convert Date to a string in the reuqired format but that will be converted to a 'real' date when you assign
it to the date variable.

What are you going to use afDate as/for later in the code?
 
Upvote 0
Norie, thank you for your reply.

It odd that you should say that, because the following code does format afDate correctly, as I would expect it (unless I have misunderstood you).

Code:
Dim afDate As Date
Format(Date, "mm/dd/yyyy")
This is the same as the previous code, except without the + 7 days. In this, afDate is ouput in the required format, i.e. 07/10/2011 (instead of 10/07/2011).

afDate is used for an AutoFilter. I have two subs and my main goal is to apply a filter to a sheet, with one sub filtering all rows with todays days (<=Date), and the second filtering rows with a date within the next seven days (<= Date + 7).

The dates are stored in the sheet as dd/mm/yyyy, but unless I format Date to an American format, the filter doesnt work.

Thank you again.
 
Upvote 0
The Format function returns a string, so afDate should be declared as a string, i.e.

Dim afDate As String
 
Upvote 0
You don't need Format at all. A date in Excel is a number, it has no format until you represent it with a string.

Code:
Dim afDate As Date
afDate = Date + 7
 
Upvote 0
shg, thats exactly what I would have thought, but as explained in my previous post, for some reason the AutoFilter wouldn't find the relevant records unless I formatted the date.

It was only after hours of searching I came across this thread (third post down) and after formatting the date, got it to work without any problems. It was only when I tried to add days onto the date that I encounted issues.

I've changed afDate to a string now it its working perfectly.

Thanks
 
Upvote 0
Sean

Using dates in VBA/Excel is always tricky.

Your code is actually converting from date to string, via the Format function, then back to date when you assign to the variable.
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,794
Members
452,943
Latest member
Newbie4296

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