How to get months in German in VBA? - excel

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!

Related

How to use Save As function?

I'm attempting to have my workbook save as a macro-enabled workbook upon execution of my macro. When the macro is initiated, a userform will populate where the user can select a FiscalYear, FormYear, and a FormMonth. The reason for separate years is because FiscalYear will begin in Oct. Oct will be year 18, however it will begin FY19.
I am attempting to insert the value of the FiscalYear into my SaveAs function. The filepath stops after I use FiscalYear and it places the remaining string from the path in front of the DocName I am wanting the workbook to saveas:
Path "J:\x\y\z\FY" & FiscalYear & "\Templates FY" & FiscalYear
DocName:"G22 Dashboard & " " & FormMonth & " " & "FY" & FiscalYear
The document will save in location "FY & FiscalYear &" as "Templates FY18G22 Dashboard & " " FormMonth & " " & "FY" & FiscalYear
Any advice to show me what I am doing incorrectly in this situation?
Probably a rookie mistake, but any help would be greatly appreciated, thanks!
I have attempted different syntax strategies (use of "" locations, & additions/removals).
Sub Save_Report_As()
'Disabling Display Alerts
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Path = "J:\x\y\z\FY" & FiscalYear & "\Templates FY" & FiscalYear
DocName = "G22 Dashboard" & " " & FormMonth & " " & "FY" & FiscalYear
ActiveWorkbook.SaveAs filename:=Path & DocName, FileFormat:=52
'Enabling Display Alerts
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Will post my comment as an answer so this will stop showing up on the "unanswered" list:
path & "\" & docname & ".xlsx" would be more appropriate... you left out the "\" between the path\docname plus the extension (#RyanWildry caught the extension, too)

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"

Prevent date formatting in VBA?

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)

Resources