Saving an excel file by using a macro

Amaarouf

New Member
Joined
Oct 10, 2002
Messages
8
Hi,

I'm new to VBA, but I'm trying to create a macro that will save an excel file as a cell name. The code that I have written so far is:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisFile = Range("V4").Value
ActiveWorkbook.SaveAs Filename:=ThisFile
ActiveWorkbook.SaveAs Filename:="W:SharedDummyMTC Tracking SheetMTC" & ThisFile
End Sub

The problem is when I place this code in 'ThisWorkbook' under BeforeSave I get the following error msg:

'Excel.exe has generated errors and will be closed by Windows. You will need to restart the program, an error log has been created'

does anyone know what I might be doing wrong?

Thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
What is in cell V4?

Do you really want to save the workbook twice - once in the current directory and then in W:SharedDummyMTC Tracking SheetMTC?
 

Amaarouf

New Member
Joined
Oct 10, 2002
Messages
8
Hi,

In V4 is an equation

=Sheet15!A1

I guess I only really need to save it once in W:SharedDummyMTC Tracking SheetMTC

thanks
 

Iridium

Well-known Member
Joined
Jul 15, 2002
Messages
2,831
Try this:
Public Sub SaveAsA1()
'Saves file as what is in cell "A1" to specified network location

Dim Location, ThisFile As String

Location = "\directorysubdirectory"
ThisFile = Range("A1")
ThisFile = Location + ThisFile
ActiveWorkbook.SaveAs Filename:=ThisFile
End Sub

Chris;)
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092

ADVERTISEMENT

What is the result of the formula:

=Sheet15!A1

in cell V4?
 

klb

Well-known Member
Joined
Apr 3, 2002
Messages
821
I have this in a module in my personal.xls. It will save the file to whatever name you have in cell A1. It will save it to the current directory unless you have a full path in A1.

Public Sub SaveAsA1()
ThisFile = Range("A1").Value
If ThisFile = "" Then Exit Sub
ActiveWorkbook.SaveAs FileName:=ThisFile

End Sub
 

Forum statistics

Threads
1,144,438
Messages
5,724,361
Members
422,546
Latest member
KevinOnTheDaily

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
Top