Copying specific worksheets to a new workbook

lcp3

New Member
Joined
Oct 21, 2022
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
I'm trying to take specific worksheets from an existing workbook and move them to a new workbook, but running into issues. In the below snip, I want to move the Lineup tab, along with Ship 1 - Ship 12 and PO Recap tabs to a new workbook, but exclude COGS, PWC and Test.UPC tabs.
1666362142759.png


Below is where I'm currently at, but obviously missing a few steps. Any suggestions?

Sub CopySheetsToNewWorkbook()
'Copy sheets to a new Workbook.

Dim numSheets As Integer
numSheets = Application.Sheets.Count - 3
Dim aSheets() As String
ReDim aSheets(numSheets - 1)


For i = 0 To numSheets - 1
aSheets(i) = ActiveWorkbook.Worksheets(i + 1).Name
Next i
ActiveWorkbook.Worksheets(aSheets()).Select
ActiveWorkbook.Worksheets(aSheets()).Copy
ActiveWorkbook.Worksheets(aSheets()).Select

Range("A1:BZ1176").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayStatusBar = False

ActiveSheet.Select

Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayStatusBar = True

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi lcp3,

what about

VBA Code:
Sub MrE1612613()
'https://www.mrexcel.com/board/threads/copying-specific-worksheets-to-a-new-workbook.1219951/
Dim arr()
Dim lngCount As Integer
Dim lngArr As Integer

For lngCount = 1 To Worksheets.Count
  Select Case Worksheets(lngCount).Name
    Case "COGS", "PWC", "Test.UPC"
    Case Else
      lngArr = lngArr + 1
      ReDim Preserve arr(1 To lngArr)
      arr(lngArr) = Worksheets(lngCount).Name
    End Select
Next lngCount

Worksheets(arr).Copy
End Sub

Ciao,
Holger
 
Upvote 0
That worked, Thank You! One more wrinkle, I'd like to paste values only.
 
Upvote 0
Hi lcp3,

maybe

VBA Code:
Sub MrE1612613_V2()
'https://www.mrexcel.com/board/threads/copying-specific-worksheets-to-a-new-workbook.1219951/
Dim arr()
Dim lngCount As Long
Dim lngArr As Long
Dim ws As Worksheet

For lngCount = 1 To Worksheets.Count
  Select Case Worksheets(lngCount).Name
    Case "COGS", "PWC", "Test.UPC"
    Case Else
      lngArr = lngArr + 1
      ReDim Preserve arr(1 To lngArr)
      arr(lngArr) = Worksheets(lngCount).Name
    End Select
Next lngCount

Worksheets(arr).Copy

For Each ws In Worksheets
  With ws.UsedRange
    .Value = .Value
  End With
Next ws
End Sub

or

VBA Code:
Sub MrE1612613_V3()
'https://www.mrexcel.com/board/threads/copying-specific-worksheets-to-a-new-workbook.1219951/
Dim arr()
Dim lngCount As Long
Dim lngArr As Long
Dim ws As Worksheet

For lngCount = 1 To Worksheets.Count
  Select Case Worksheets(lngCount).Name
    Case "COGS", "PWC", "Test.UPC"
    Case Else
      lngArr = lngArr + 1
      ReDim Preserve arr(1 To lngArr)
      arr(lngArr) = Worksheets(lngCount).Name
    End Select
Next lngCount

Worksheets(arr).Copy

For Each ws In Worksheets
  ws.UsedRange.Copy
  ws.Range("A1").PasteSpecial xlPasteValues
Next ws
End Sub

Ciao,
Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,214,994
Messages
6,122,633
Members
449,092
Latest member
bsb1122

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