Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Need to copy sheet from a macro workbook and save as .xlsx

0

i can use the following and it works

sheets(array("Sheet1", "Sheet2", "sheet3")).copy

i can save as .xlsx 

That works perfect using sheet names

How can i do same using sheet code name e.g

sheets(array("wsTasks", "wsAction", "wsDependency")).copy

this does not work, 

Answer
Discuss

Answers

0
Selected Answer

Semaj

In the attached file, I set our VBA sheet CodeNames ( to "wsTasks", "wsAction", "wsDependency") as they were added. In Module1, I changed your code to use .Name property (and removed the quotation marks (") to give this working code:

Sub CopySheetsByCodeName()

    Sheets(Array(wsTasks.Name, wsAction.Name, wsDependency.Name)).Copy

End Sub

Now the user can change the worksheet names e.g from "Teach", "Excel", "Demo" to whatever (or alter their sequence) and the code will still copy those sheets.

I assume you will prevent the user opening the file without macros-enabled and wil prevent them from deleting any of these sheets (if not, you'll find methods in the Tutorials section).

Hope this is what you anted. If so, please mark this Answer as Selected.

Discuss

Discussion

This worked perfectly thanks John
semaj (rep: 10) Dec 23, '24 at 7:04 am
Great! Thanks for selecting my Answer Semaj. 
John_Ru (rep: 6607) Dec 23, '24 at 10:56 am
Add to Discussion
0

Hello semaj,

I'm a bit confused in how you are describing worksheet names. In the VB Editor the worksheets (Microsoft Excel Objects) are listed as: SheetCodeName(worksheet tab name) – example: Sheet1(Data). Without a sample file it's difficult to see what is what. In your first line of code to copy it appears that you are using worksheet code names and in your second line of code to copy it appears that you are using variables that have been assigned to each worksheet (ex: wsTasks).

If these are variables, try removing the quote marks  -  use wsTasks instead of "wsTasks".

If this doesn't help, upload a sample file so we can better see what is going on.

Cheers   :-)

Discuss

Discussion

when i use the codenames of the sheets i am unable to create the array. It only works if i use the tab names. The question is can i make the array using sheet codenames somehow. If user changes tab name my code will break
semaj (rep: 10) Dec 20, '24 at 1:52 am
@Semaj - I just added an Answer for you to consider. Bit late but I was out of my country for a few days.

@Willie - thanks- your Answer precipiated the point above (which gave me a better clue of what was needed).

Seasons Greetings / Happy Holidays/ Merry Christmas to you!
John_Ru (rep: 6607) Dec 20, '24 at 4:02 am
@John -  I was waiting for semaj to respond before I put any more effort into this. If a poster truly wants an answer they will provide feedback in a timely manner. You have taken the same approach I was going to use had semaj responded sooner.

Merry Christmas and Happy New Year   :-)
WillieD24 (rep: 657) Dec 20, '24 at 1:47 pm
Merry Christmas and Happy New Year   :-)
semaj (rep: 10) Dec 23, '24 at 10:06 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login