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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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