Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Jody Mills   on Oct 21 In MS Office Category.

  
Question Answered By: Muhammad Evans   on Oct 21

I've been trying to ignore email these last few weeks because my tact and my
interest have both evaporated. So if I provide offense in my reply, please
know it's a personal limitation and nothing personal beyond that.

I think I'd take a slightly different approach on this. Get your filename
created in a separate function. The function should do nothing more than:
- create the date  based file  name (this way you can introduce other
functions to easily allow you to change paths if you'd like)
- If a month, day or second value is less than 2 characters, add a leading
zero

Here are the basics.
'============================================================
Function GetDateBasedFileName()

Dim strYear As String
Dim strMonth As String
Dim strDay As String
Dim strVer As String

strYear = Year(Now)
strMonth = Pad(Month(Now))
strDay = Pad(Day(Now))
strVer = Pad(Second(Now))

GetDateBasedFileName = strMonth & strDay & strYear & strVer

End Function
'============================================================
Function Pad(strToPad)

If Len(strToPad) < 2 Then
Pad = "0" & strToPad
Else
Pad = strToPad
End If

End Function
'============================================================

You can test this with the Immediate Window open in your VBE and running
this test macro:
Sub Main()
Debug.Print GetDateBasedFileName
End Sub

So your date creation formula below would now look like this:
ActiveWorkbook.SaveAs Filename:="C:\" & GetDateBasedFileName & ".xls"

You could also create other functions to ensure you have write permissions
to that path but that's another topic. Just know that the newer security
features of Vista are going to give you some challenges about writing to
some directories including the drive root.

Share: 

 

This Question has 4 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on using todays date as file name Or get search suggestion and latest updates.


Tagged: