VBA: create new workbook, rename, copy sheets from original to new with exceptions

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,934
Office Version
  1. 365
  2. 2010
From a VBA routine, I want to create a new workbook, rename it (so that I can date stamp the name), then copy sheets from original workbook except certain sheets (only 3 not copied).
Then delete the copied sheets from the original workbook.

I've been messing with this for over an hour and can't get it right. Thanks in advance.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Please try it with a workbook which has at least four sheets.

VBA Code:
Sub test()
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Move
ActiveWorkbook.SaveAs Filename:="C:\Users\" & Environ("username") & "\Desktop\" & "ABC" & Format(Date, "mmyyyy") & ".xlsx"
ActiveWorkbook.Close
End Sub
 
Upvote 0
Thanks Takae for your suggest. My issue is that I don't know which sheets by name will be moved because it changes every time the user runs the routine.
 
Upvote 0
How would a macro determine which sheets should be copied & which should be deleted?
 
Upvote 0
The three not to be deleted are always the same three.
The ones to move and delete vary but are named in a sheet called "TextFile" in the range S5 to Sn
 
Upvote 0
BTW: if it matters, I need to move the sheets and also retain their formatting (especially the column width).
 
Upvote 0
Ok, how about
VBA Code:
Sub kweaver()
   Dim UsdRws As Long, i As Long
   Dim Cl As Range
   Dim Ary As Variant
   
   With Sheets("TextFile")
      UsdRws = .Range("S" & Rows.Count).End(xlUp).Row
      ReDim Ary(UsdRws - 5)
      For Each Cl In .Range("S5:S" & UsdRws)
         Ary(i) = Cl.Value
         i = i + 1
      Next Cl
   End With
   Sheets(Ary).Move
End Sub
 
Upvote 0
Fluff: thanks. I haven't tried it yet, but shouldn't this macro create the new workbook and then move those in the array (Ary) to it?

Just tried it and the new sheet has the typical "Book" name to it. How do I then rename it and save that workbook?
 
Upvote 0
It will create the new workbook automatically.
 
Upvote 0
RIGHT! That did it! As usual, you have come to my rescue! Much appreciated.
AND, I have learned something else about using arrays. Double benefit of your expertise.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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