Prevent date formatting in VBA? - excel

I have a piece of code where a workbook should be saved as file path + prefix + whatever date the user entered into user unput box. The date enter should be in the format of MMDDYYYY. The problem is VBA converts the date and saves it as MM-DD-YYYY format. How do I prevent this.
My code:
With prevdaily
Application.DisplayAlerts = False
' Check for year folder and create if needed
If Len(Dir("G:\AccPac ERP Daily Reports\" & Year(Date), vbDirectory)) = 0 Then
MkDir "G:\AccPac ERP Daily Reports\" & Year(Date)
End If
' Check for month folder and create if needed
If Len(Dir("G:\AccPac ERP Daily Reports\" & Year(Date) & "\" & Format(Date, "mm ") & MonthName(Month(Date), False), vbDirectory)) = 0 Then
MkDir "G:\AccPac ERP Daily Reports\" & Year(Date) & "\" & Format(Date, "mm ") & MonthName(Month(Date), False)
End If
fname2 = InputBox("Please enter date of Daily Income Journal to save")
fname2 = fname2 & ".xlsx"
fpath = "G:\AccPac ERP Daily Reports\" & Year(Date) & "\" & Format(Date, "mm ") & MonthName(Month(Date), False) & "\"
ActiveWorkbook.SaveAs FileName:=fpath & "Daily Income Journal-" & fname
Thank you Barranka!

I think you can use the format() function to create a string with the desired format:
Example:
Dim strDate as String
strDate = format(Date, "mmddyyyy")
Read:
MSDN: Format funnction (VBA)

Related

How to get months in German in VBA?

I need my code to create a folder named with german month:
not 05 March
but 05 März
Dim strMonth As String
strMonth = Format(Date, "mm")
' Check for month folder and create if needed
If Len(Dir("C:\Users\Luca\Desktop\TestDaty\" & client & "\" & Year(Date) & "\" & strMonth & " " & MonthName(Month(Date), False), vbDirectory)) = 0 Then
MkDir "C:\Users\Luca\Desktop\TestDaty\" & client & "\" & Year(Date) & "\" & strMonth & " " & MonthName(Month(Date), False)
End If
so maybe some country code here?
MonthName(Month(Date)
Please help me!!! :)
If your locale is set to German I guess something like Format(Date, "mmmm") should do (works for me). Otherwise, if you just need to create German named folders you can use a language-code (LCID) in combination with WorksheetFunction.Text():
WorksheetFunction.Text(Date, "[$-407]mmmm")
Where [$-407] is the language-code to return a German monthname!

Variable not being consistant in excel vba macro

I have a macro in excel that if a drive exists the macro saves the file to my harddrive and thumbdrive. If it doesn't exist, it saves to the harddrive. When the macro runs I am getting an error. Here is the macro:
Sub SaveFile()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim filepath As String
name = "Siemens"
filepath = "F:\Dave backup\Open Orders\Label Manifests\Active Labels Manifest\Manifest Related\File saving testing folder\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
If fso.DriveExists("F:\") = True Then
'ActiveWorkbook.SaveAs filename:="C:\Users\dgray\Documents\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
'ActiveWorkbook.SaveAs filename:="F:\Dave backup\Open Orders\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
ActiveWorkbook.SaveAs filename:=filepath
Else
'ActiveWorkbook.SaveAs filename:="C:\Users\dgray\Documents\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
ActiveWorkbook.SaveAs filename:="F:\Dave backup\Open Orders\Label Manifests\Active Labels Manifest\Manifest Related\File saving testing folder\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
End If
End Sub
Here is the error I am getting:
I don't know if you can see but the last part of the error message says "\Siemens\8E555720. That should also say the customer name (i.e. Siemens). In the code I have set the customer name in the variable "name". So why is it giving me this crazy error? All help is appreciated.
Something like this might be better:
Sub SaveFile()
Const PATH_C As String = "C:\Users\dgray\Documents\"
Const PATH_F As String = "F:\Dave backup\Open Orders\Label Manifests\" & _
"Active Labels Manifest\Manifest Related\File saving testing folder\"
Dim fileName As String, custName As String
custName = "Siemens"
fileName = custName & " Manifest " & Format(Now, "mm-dd-yyyy") & ".xlsx" 'or .xlsm
ActiveWorkbook.SaveAs fileName:=PATH_C & fileName 'assume C is always available
'save to F if available
If Len(Dir(PATH_F)) > 0 Then
'assumes the custName folder already exists...
ActiveWorkbook.SaveAs fileName:=PATH_F & custName & "\" & fileName
End If
End Sub
I can see the space in folder name which may cause this error.
By removing space in the foldername this error would be fixed.

WorksheetFunction.Month error with object doesn't support property or method

I am trying to save a file into folder & sub folder that has dynamic names.
Structure of my folder & sub folder is
Shared Drive -> Main -> Year (i.e. 2018) -> Month (i.e. 09 September).
I wish to save in last month's folder since data comes from last month. I want to save the file name as PIF_09.30.2018 so using end of last month's dates.
I am getting an error
object doesn't support this property or method.
Sub Save_wkb()
Dim Path As String
ActiveWorkbook.SaveAs "S:\Main\" & Format(Now(), "YYYY") & _
Application.PathSeparator & Format(WorksheetFunction.Month(Now, -1), "MM ") & _
Format(WorksheetFunction.Month(Now, -1), "MMMM") & _
Application.PathSeparator & "PIF_" & _
Format(WorksheetFunction.EoMonth(Now, -1), "MM.D.YYYY") & ".xlsx", _
FileFormat:=51
End Sub
You are not able to use WorksheetFunction.Month - it doesn't exist.
Since you are trying to subtract a month, you can use the DateAdd() function instead.
Dim Path As String
Path = "S:\Main\" & Format(Now(), "YYYY") & Application.PathSeparator & _
Format(DateAdd("m", -1, Date), "00 ") & Application.PathSeparator & _
Format(DateAdd("m", -1, Date), "MMMM") & Application.PathSeparator & _
"PIF_" & Format(WorksheetFunction.EoMonth(Now, -1), "M.D.YYYY") & ".xlsx"

Why can't Excel 365 vba save my pdf file?

I think now I tried nearly everything to save a pdf file with Excel vba.
This is my actual code:
Dim pdfFile As String
If excel2016 Then
pdfFile = Application.DefaultFilePath & "/Report_" & year & month & "_" & Replace(name, " ", "_") & ".pdf"
Else
pdfFile = ActiveWorkbook.Path & Application.PathSeparator & "Report_" & year & month & "_" & Replace(name, " ", "_") & ".pdf"
End If
wsOutputOne.ExportAsFixedFormat Type:=xlTypePDF, fileName:=pdfFile
I know that Excel 2016 is creating a "container" and saving the files under ~/Library/Containers/com.microsoft.Excel/Data/Documents/ but why does that code not work with Excel 365?
Or what has a friend with Excel 365 to do to save this generated pdf file?
Because the call of Application.DefaultFilePath returns nothing on his machine...
Can somebody help me?
Please try the following code:
Dim pdfFile As String
If excel2016 Then
pdfFile = Application.DefaultFilePath & "/Report_" & year & month & "_" & Replace(name, " ", "_") & ".pdf"
Else
pdfFile = Application.ActiveWorkbook.Path & Application.PathSeparator & "Report_" & year & month & "_" & Replace(name, " ", "_") & ".pdf"
End If
wsOutputOne.ExportAsFixedFormat Type:=xlTypePDF, fileName:=pdfFile

VBA way to manually save the excel file based on 'dynamic' parameters passed.

I have created an application using excel macro, where the user feeds certain values and saves it to several directory path with a button click macro.
When I select a region from drop down, it should save the file to designated region folder. Say for eg, when NY is selected, the file will be saved to shared drive and 2016 - NY folder. But now, deciding the future of the application, I am thinking of having "year" as a separate field in the worksheet, which retrieves the year value from the user. How do I achieve this without the necessity to change the code every year. The process will be continuing for 'n' number of years from now. Thanks in Advance !
FileName1 = Range("D3").Value
filenameOfNewBook = FileName1
If location = "Illinois" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\Illinois\" & FileName1 & "-" & "checklist" & ".xlsm"
ElseIf location = "LA" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\LA\" & FileName1 & "-" & "checklist" & ".xlsm"
ElseIf location = "NY" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\NY\" & FileName1 & "-" & "checklist" & ".xlsm"
Else
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\Atlanta\" & FileName1 & "-" & "checklist" & ".xlsm"
End If
MsgBox "File Saved successfully!", , "Save"
ActiveWorkbook.save
Application.DisplayAlerts = True
From my experience for office tasks purpose, it's better not to refer to current year, but year set by user, so for example in January 2017 user can still perform actions on files related to 2016. You can get rid of the following:
If location = "Illinois" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\Illinois\" & FileName1 & "-" & "checklist" & ".xlsm"
ElseIf location = "LA" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\LA\" & FileName1 & "-" & "checklist" & ".xlsm"
ElseIf location = "NY" Then
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\NY\" & FileName1 & "-" & "checklist" & ".xlsm"
Else
ActiveWorkbook.SaveAs FileName:="W:\Audits\2016\Atlanta\" & FileName1 & "-" & "checklist" & ".xlsm"
End If
And instead use:
Dim myYear as String, locations() as String, locationForPath as String
Dim locCounter as Long
myYear = ThisWorkbook.Worksheets("Sheet1").Range("a2").value2 'the cell with year value, for example 2016
locations = Split("Illinois,LA,NY",",")
For locCounter = LBound(locations) to UBound(locations)
If location = locations(locCounter) Then locationForPath = location: Exit For
Next locCounter
If locationForPath = vbNullString Then locationForPath = "Atlanta"
ActiveWorkbook.SaveAs FileName:="W:\Audits\" & myYear & "\" & locationForPath & "\" & FileName1 & "-" & "checklist" & ".xlsm"

Resources