Save a Worksheet with a String Name

Vlr516

New Member
Joined
Sep 11, 2017
Messages
22
Hi,
I am trying to save a new worksheet using a value in my sourcesheet and the tab name of the sourcesheet. My code works well if I just use the value, but as soon as I add the sourcesheet name code, I get an error. If I just do the A1 value it worksCan you help?

Code:
Sub NameNewWorksheet

Dim Name as String
Dim sourcesheet As Variant

activesheet.copy
Name = sourcesheet.range("A1").Value & " " & sourcesheet.Name     
ActiveWorkbook.SaveAs Filename:=Name

End Sub
[\Code]
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I believe the issue is that "Name" is a reserved word, it is an existing Property for workbook and worksheet objects. You are actually using it in your setting the name variable!!!
So you are causing confusion for VBA. The rule of thumb is NEVER used reserved words (like names of existing Objects, Properties, Functions, etc) as variable names. It can cause ambiguity, errors, and unexpected results.

You also have created a variable for "sourcesheet", but have not set it to anything yet!

Try something like:
Code:
[COLOR=#333333]Sub NameNewWorksheet[/COLOR]

[COLOR=#333333]Dim NewName as String[/COLOR]
[COLOR=#333333]Dim sourcesheet As Worksheet
[/COLOR]
'Set sourcesheet variable here

[COLOR=#333333]activesheet.copy[/COLOR]
[COLOR=#333333]NewName = sourcesheet.Range("A1").Value & " " & sourcesheet.Name [/COLOR]
[COLOR=#333333]ActiveWorkbook.SaveAs Filename:=NewName[/COLOR]

[COLOR=#333333]End Sub[/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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