Excel macro to always save file to My Documents

Alwinaz

Board Regular
Joined
Feb 7, 2012
Messages
201
Hi there

I am using this code to save my file by creating a foloder and then saving it with the name specified in cell A21. This works, however it saves it to source i am using it from. For example, if i try to save it from an attachment in outlook, it saves it to my outlook folder. How do i change this code to always save in in the users My Documents Folder. Here is the code i am using.

Code:
Public Sub SaveToDir()
CDir = ActiveWorkbook.Path
SaveDir = CDir & "\" & ActiveSheet.Range("A21")
'
'check to see if Dir exists if not create it. Could also abort if the Dir should exist
If Len(Dir(SaveDir, vbDirectory)) = 0 Then
   MkDir SaveDir
End If
'
'Checks to see if the Date cell is in date format
'If IsDate(ActiveSheet.Range("B1")) Then
 '   SaveName = ActiveSheet.Range("A1") & "_" & Application.Text(ActiveSheet.Range("B1"), "DD-MMM-YYYY") & ".xlsm"
'Else
    SaveName = ActiveSheet.Range("A21") & ".xlsm"
'End If
'
'Check to see if the file already exists
If Len(Dir(SaveDir & "\" & SaveName, vbDirectory)) > 0 Then
    Resp = MsgBox("File name:   " & SaveName & vbCrLf & vbCrLf & "already exists in:  " & vbCrLf & vbCrLf & SaveDir & vbCrLf & vbCrLf & "Press Okay to continue, Cancel to abort", vbOKCancel)
End If
'
If Resp = vbCancel Then
    Exit Sub
End If
Application.DisplayAlerts = False
    Sheets("Instructions").Copy
    ActiveWorkbook.SaveAs Filename:= _
        SaveDir & "\" & SaveName, FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
  '  ActiveSheet.Shapes("Button 1").Cut
    
    
    ActiveWindow.Close
    
MsgBox ("File name:   " & SaveName & vbCrLf & vbCrLf & "has been saved to  " & vbCrLf & vbCrLf & SaveDir)
        
999   End Sub

Thanks so long
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Why are you copying a single worksheet? When you do that Excel adds a new workbook and that becomes the ActiveWorkbook, which is what you are saving.
 
Upvote 0
I dont want to copy. I want to create a folder under my documents (Funmathics) and then save the file with the file name in A21.
 
Upvote 0

Forum statistics

Threads
1,214,877
Messages
6,122,051
Members
449,064
Latest member
scottdog129

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top