Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Issues with Activeworkbook.saveas

0

I am trying to use the BefroeClose event to trigger a SaveAs. The code mostly works but it is going to the worng directory. The workbook is being saved as C:\Users\bkrukowski and has a file name of DesktopA12345667_555Main. Code Below

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim MyPath As String, myName As String
If Len(Worksheets("Sheet1").Range("B3").Value) < 1 Or Len(Worksheets("Sheet1").Range("D3").Value) < 1 Then
Cancel = True
Exit Sub
Else
MyPath = Application.ActiveWorkbook.Path
myName = Worksheets("Sheet1").Range("B3").Value & "_" & Worksheets("Sheet1").Range("D3").Value

ActiveWorkbook.SaveAs Filename:=MyPath & myName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End If

So I changed the SaveAs to

ActiveWorkbook.SaveAs Filename:=MyPath & "\Desktop"& myName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

The file goes to the desktop but the file name now starts with Desktop

Please do not use the Generate Patch Details button if you have Outlook Open as I will receive an email with the file.

Answer
Discuss

Answers

0

Try changing "\Desktop" to this: "\Desktop\"

Discuss

Discussion

After changing to "\Desktop\" i get a run time error the dreded 1004 error. Possible reasons File name or path does not exist, fiel name being used by another program or the workbook you are trying to save has the same name as a ...
The debugger puts the error on
ActiveWorkbook.SaveAs Filename:= MyPath & "\Desktop\" & myName 


Sorry I am  attahcing a screen shot of the error message.
BJKruk (rep: 2) Aug 5, '16 at 3:17 pm
You want this to save to your desktop right?
don (rep: 1989) Aug 5, '16 at 3:23 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login