PDA

View Full Version : cannot add date



exploringex
03-04-2016, 02:21 PM
Hi, the macro keeps giving me "run-time error 13". the highlighted in green has issue. I don't know how to resolve it. I need the file name in "yyyymmdd" date format. could anyone help. Thanks


Dim strLocation As String
Dim FolderYear As String
Dim FolderMonthYear As String
Dim FileDate As String
Dim FileDate2, FileDate3, FileDate4 As String
Dim FileDate5, FileDate6, FileDate7 As String
Dim FileDateSubject As Date
Dim UserName, Contact_Info As String

Dim a As String


FolderYear = Format(Now(), "yyyy")
FolderMonthYear = Format(Now(), "mm-yyyy")
FileDate = Format(Now(), "yyyymmdd")
FileDateSubject = Format(Now(), "mm/dd/yyyy")


FileDateSubject = DateAdd("d", 1, FileDate) 'Green highlight here. SamT

FileDate2 = Format(DateAdd("d", 1, FileDate))

FileDate3 = Replace(FileDate2, "/", "-") 'replace slashes with dashes
FileDate4 = Format(FileDateSubject, "yyyymmdd") 'change File Date to format "yyyymmdd"



If IsWeekend(FileDate4) = True Then FileDate4 = DateAdd("d", 3, FileDate) 'weekend test
FileDate6 = FileDate4

FileDate6 = Replace(FileDate4, "/", "-")
FileDate7 = Format(FileDate4, "mm-dd-yy")
FileDate4 = FileDate7
FolderMonthYear = Format(FileDate4, "mm-yyyy")

Paul_Hossler
03-04-2016, 03:48 PM
FileDate is a string and FileDateSubject is a Date

Format() returns a formatted string


Try something like this




Option Explicit
Sub test()

Dim FileDate As String
Dim FileDateSubject As String

FileDate = Format(Now(), "yyyymmdd")
FileDateSubject = Format(DateAdd("d", 1, Now()), "yyyymmdd")

End Sub