Copying Certain Sheets from Active WB and creating New WB

honger

New Member
Joined
Dec 12, 2013
Messages
22
Hello All,

I currently have macro that creates new WB in a designated drive.

I also have a combobox which decides which sheets to copy. So for example, if master is selected, it will copy sheeets 1,5,2 and if template1 is selected it will copy sheets 7,5,2. etc

THis is the current code i have but i am getting error on the Sheets(Array( portion of the code. Can someone help me out what I am doing wrong? i think the line with RED fonts are the problem as macro runs fine without that line and combobox3.

Code:
Dim vFilename As Variant
Dim vPath As Variant
vpath1 = "[URL="file://\\tarcds01\eCTD_Submission\TRACKERS\"]\\tarcds01\eCTD_Submission\TRACKERS\[/URL]" & MyProduct & "\" & MyApplication & "\"
vpath2 = MyProduct & "-" & MyApplication & "-" & MySCN & "-" & MyEvent

[COLOR=#ff0000]If ComboBox3.Text = "Master" Then Sheets(Array("Sheet1", "Sheet5", "Sheet2")).Copy
[/COLOR][COLOR=#000000]ActiveWorkbook.SaveCopyAs Filename:=vpath1 & vpath2 & ".xlsm"

[/COLOR]If ComboBox3.Text = "Template1" Then Sheets(Array("Sheet7", "Sheet5", "Sheet2")).Copy
ActiveWorkbook.SaveCopyAs Filename:=vpath1 & vpath2 & ".xlsm"

'Reference the new workbook under  a friendly name
    Set newWBK = Workbooks.Open(vpath1 & vpath2 & ".xlsm")
'Make the new workbook active
    newWBK.Activate
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
What is the error message you are getting? And when you click on debug, does the line in RED appear as highlighted in your code?
 
Upvote 0
i am getting Run-time error 9. Subscript out of range, and when i debug, the below code gets highlighted. I am very new to vba, thank you for your patience

Code:
Sheets(Array("Sheet1", "Sheet5", "Sheet2")).Copy
 
Upvote 0
Try defining each array and then using the variable as the object to copy.

For example
Code:
Dim Arr1 as Array

Arr1=Array("Sheet1","Sheet5",Sheet2")

other code

If combobox3.text = "Master" then Sheets(Arr1).Copy

more code
 
Upvote 0
Thank you for the quick reply, i am getting this error for "Array" while i am typing Dim Arr1 as Array

Compile error: Expected: New or Type name
 
Upvote 0
So i replaced Array with myArray and it took it, however now i am getting

compile error: extected: )

Code:
Arr1 = ("sheet1","sheet2","sheet5")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,786
Messages
6,132,698
Members
449,753
Latest member
swastikExcel

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