NormChart55
New Member
- Joined
- Feb 22, 2022
- Messages
- 44
- Office Version
- 2016
- Platform
- Windows
Hello,
I am trying to tweak this code below to instead of asking for a new name of the file, to choose a new location to save the file to. Basically, some users will have one drive active and others will be using their personal desktop to save files. So I am trying to automatically save to personal desktop but when they do not have the User folder/using one drive it allows them to press 'Yes' and choose another location for the file? The 'Path = "C:\TEST\" is not available and only being used to test if the prompts work.
I am trying to tweak this code below to instead of asking for a new name of the file, to choose a new location to save the file to. Basically, some users will have one drive active and others will be using their personal desktop to save files. So I am trying to automatically save to personal desktop but when they do not have the User folder/using one drive it allows them to press 'Yes' and choose another location for the file? The 'Path = "C:\TEST\" is not available and only being used to test if the prompts work.
VBA Code:
Dim Path As String
Dim FileName As String
Dim dt As String
dt = Format(CStr(Now), "yyyy_mm_dd_hh_mm")
user = Environ("Username")
Path = "C:\TEST\"
Desktop = "C:\Users\" & user & "\Desktop\"
FileName = "Backorder"
ActiveSheet.Range("C5:H50000").Copy
Set NewBook = Workbooks.Add
NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("A:E").Select
Columns("A:E").EntireColumn.AutoFit
On Error GoTo NoSave
ActiveWorkbook.SaveAs FileName:=Path & FileName & " " & dt & ".xls", FileFormat:=xlNormal
NoSave:
On Error GoTo 0
Application.EnableEvents = True
Answer = MsgBox("Cannot save the file as it appears the folder and/or desktop path is not available." & _
Chr$(13) & "Do you wish to manually save?", vbYesNo, ThisWorkbook.Name)
If Answer = vbNo Then
'Code for No button Press
MsgBox "You pressed NO!"
Else
'Code for Yes button Press
NewFilename = InputBox("Please enter new filename", _
"filename", "Type your filename here")
If NewFilename <> "" Then
FName = NewFilename
Resume
End If
End If