Johan Larsen
Johan Larsen

Reputation: 13

Saving a xlsm file with the name of specific cell through vba macro

I'm trying to make a button that saves my workbook with the name of a specific cell from one of its own sheets.

The cell that I'm getting the name from is gonna change it's value each week. I get the error:

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

Here is the code:

Private Sub Save_file_Click()
Dim path As String
Dim filename1 As String
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Fejlregistrering")

path = "C:\Users\JOHLA\Desktop\Yield ark\"
filename1 = ws.Range("D5").Text
ActiveWorkbook.SaveAs path & filename1 & ".xlsm", FileFormat =  xlOpenXMLWorkbookMacroEnabled


End Sub

//Johan

Upvotes: 0

Views: 1731

Answers (2)

Karthick Gunasekaran
Karthick Gunasekaran

Reputation: 2713

try with below

ActiveWorkbook.SaveAs Filename:=path & filename1, FileFormat:=xlOpenXMLWorkbookMacroEnabled

Upvotes: 0

Kevin Mueller
Kevin Mueller

Reputation: 668

Change

ActiveWorkbook.SaveAs path & filename1 & ".xlsm", FileFormat = xlOpenXMLWorkbookMacroEnabled

to

ActiveWorkbook.SaveAs Filename:=(path & filename1 & ".xlsm"), FileFormat:=xlOpenXMLWorkbookMacroEnabled

Upvotes: 1

Related Questions