Save As Button

Apeman

New Member
Joined
Nov 29, 2018
Messages
7
I am trying to write some VBA so create a save as button that saves the document into the correct location and creates the correct file name... I am a bit of a VBA noob, I can do basics but this has me stumped!:confused::confused: Please see the code below that I have tried to butcher from various different sources. Apologies in advance for the crap code, I know this will be easy for some of you guys to fix:

Code:
Private Sub CommandButton2_Click()


Dim fsName As String
Dim fName As Variant
Dim path As String


fsName = Range("A2").Value
fName = ("ABC123" & fsName)
path = ("bwg-apps01\cash4w\cabinet\Sales Enquiry\000" & fsName & "\001 Cost Sheet\")


ActiveWorkbook.SaveAs Filename:=(path & fName), FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False


 
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Does A2 also include the file extension .xlsm? as you maybe missing the extension. Or try adding this

ActiveWorkbook.SaveAs Filename:=(path & fName) & ".xlsm"

Not tested.
 
Last edited:
Upvote 0
Does A2 also include the file extension .xlsm? as you maybe missing the extension. Or try adding this



Not tested.

Still not working... Cell A2 just contains a 5-digit number (job ref. number).

The error message is:

"Run-time error '1004':
Method 'SaveAs' of object'_Workbook' failed"

When I Debug it heiglights the following line:
Code:
ActiveWorkbook.SaveAs Filename:=(path & fName & ".xlsm"), FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
 
Upvote 0
Code:
[COLOR=#333333]path = ("bwg-apps01\cash4w\cabinet\Sales Enquiry\000" & fsName & "\001 Cost Sheet\")[/COLOR]

You haven't specified a Drive ....
 
Upvote 0
Code:
[COLOR=#333333]path = ("bwg-apps01\cash4w\cabinet\Sales Enquiry\000" & fsName & "\001 Cost Sheet\")[/COLOR]

You haven't specified a Drive ....

Well Spotted... It is a UNC path and I missed the '\\' from the beging. However, it still doesn't work, I still get the same message, my code now looks like this:
Code:
Private Sub CommandButton2_Click()




Dim fsName As String
Dim fName As Variant
Dim path As String




fsName = Range("A2").Value
fName = ("CostSheet 000" & fsName)
path = ("\\bwg-apps01\cash4w\cabinet\Sales Enquiry\000" & fsName & "\001 Cost Sheet\")




ActiveWorkbook.SaveAs Filename:=(path & fName & ".xlsm"), FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False




 
End Sub

I also tried using the local drive letter as below and still no joy:
Code:
Private Sub CommandButton2_Click()




Dim fsName As String
Dim fName As Variant
Dim path As String




fsName = Range("A2").Value
fName = ("CostSheet 000" & fsName)
path = ("G:\cabinet\Sales Enquiry\000" & fsName & "\001 Cost Sheet\")




ActiveWorkbook.SaveAs Filename:=(path & fName & ".xlsm"), FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False




 
End Sub
 
Upvote 0
I have created the following code and it works:
Sub Macro1()
fsName = ActiveSheet.Name
sName = Sheets("CostSheet 000").Range("A2")
path = "C:\Users\xxxx\Documents"

ActiveWorkbook.SaveAs Filename:=path & fsName & sName & ".xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub

 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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