0

I have code to save a file. Here the path is static.

Sub savefile()

Dim strpath As String
Dim fry As String
Dim mth As String
Dim yr As String

yr = Year(Now)
mth = MonthName(Month(Now))
fry = Application.WorksheetFunction.Weekday(Date, 11)

Filename = "D:\Users\Desktop\ docs\" & yr & " " & "Week Of" & " " & mth & " " & fry & ".jpg"

ThisWorkbook.SaveCopyAs (Filename)

End Sub

How to replace this static or hardcode with a dynamic path?

Community
  • 1
  • 1
Shaon
  • 133
  • 1
  • 13

3 Answers3

0

Try below sub

Sub SaveFileAs()
Dim strpath As String
Dim fry As String
Dim mth As String
Dim yr As String

    yr = Year(Now)
    mth = MonthName(Month(Now))
    fry = Application.WorksheetFunction.Weekday(Date, 11)

    Filename = yr & " " & "Week Of" & " " & mth & " " & fry

Application.Dialogs(xlDialogSaveAs).Show (Filename)
End Sub
Harun24hr
  • 30,391
  • 4
  • 21
  • 36
0

The same way you have done with the year, you can allow your user to input data for the path into a variable that is a string and then use & to put the entire path togheter.

example:

Sub savefile()

Dim strpath As String
Dim fry As String
Dim mth As String
Dim yr As String
Dim path As String

yr = Year(Now)
mth = MonthName(Month(Now))
fry = Application.WorksheetFunction.Weekday(Date, 11)    

Filename = yr & " " & "Week Of" & " " & mth & " " & fry & ".jpg"
Path = Application.DefaultFilePath & yr & " " & "Week Of" & " " & mth & " " & fry & ".jpg"

Application.Dialogs(xlDialogSaveAs).Show (Filename)
ThisWorkbook.SaveCopyAs (Path)


End Sub

If you want your user to input the path directly you can use the application.dialogs(xlDialogSaveAs).Show (Filename)

If you want the program to save all the files in a predefined path that can altho change depending on the structure of the computer you are using you could use the Application.DefaultFilePath that would save to documents in my case, depending on which folder the user has set up as it's default savefile path it will save there.

NOTE: the default saving location can be changed and it will be where Excel saves files by default.

Wolfaloo
  • 94
  • 1
  • 13
0

If you share the same network drive the path should be stated as an UNC Path, for example: (\\?\C:\my_dir).

To find the UNC path, use the cmd.exe (command prompt) and write net use.

Wizhi
  • 6,424
  • 4
  • 25
  • 47