VBA code to save selected sheets to a new workbook

JonasTiger

New Member
Joined
Jan 28, 2022
Messages
24
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi
I found this thread
vba code to save selected sheets as new file
That works fine and fast to create a new WB from selected sheets in an existing WB

The changes I need to do is:
  • available to choose the sheets (not the the last 3 that the code fits) - I need to select 6 random sheets (i.e, "Sheet1", "abb", "Sheet4", ...)
  • not display zeros in allsheets in the new WB
  • Filter all sheets in the new WB, based on values of a specific column
Thanks in advance
JT
 
You could replace the sensitive data with generic data. A dozen or so rows would be enough. Please include the formulae.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi
These are screenshots of two sheets:
ORIGINAL SHEET 1
OriginalSheet.png


EXPORTED SHEET1

ExportedSheet.png


ORIGINAL SHEET2

OriginalSheet2.png

EXPORTED SHEET2

ExportedSheet2.png


All green, red and yellow colors are result of conditional formatting.
As seen, EXPORTED SHEET1 replicates colors from original file but it doesn't hide zero values. EXPORTED SHEET2 doesn't replicateclolor from original File
 
Upvote 0
Since what you posted were actually pictures and not screenshots, I was unable to test the macro on that data. Try the following:
-hold down the ALT key and press the F11 key to open the Visual Basic Editor
-in the Visual Basic Editor, click 'Insert' in the menu at the top
-insert a userform in your workbook
-in that userform , insert a command button and place it near the top of the form
-double click anywhere on the userform but not the command button
-copy/paste the following code in the empty code window that pops up
VBA Code:
Private Sub CommandButton1_Click()
    Application.ScreenUpdating = False
    Dim c As MSForms.Control, shArr() As Variant, n As Long, ws As Worksheet, v As Variant, rng As Range
    v = Array("Confirmed", "Rejected", "Waiting")
    ReDim Preserve shArr(1 To 1)
    For Each c In Me.Controls
        If TypeName(c) = "CheckBox" Then
            If c.Value = True Then
                n = n + 1
                ReDim Preserve shArr(1 To n)
                shArr(n) = c.Caption
             End If
        End If
    Next c
    Sheets(shArr).Copy
    For Each ws In Sheets
        With ws
            .UsedRange.Cells.Value = .UsedRange.Cells.Value
            .Range("A1").CurrentRegion.AutoFilter 28, v, xlFilterValues
            Set rng = ws.AutoFilter.Range
            rng.Replace 0, "", xlWhole
        End With
    Next ws
    Application.ScreenUpdating = True
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, chkBox As Control
    For Each ws In Sheets
        Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox" & i)
        chkBox.Caption = ws.Name
        chkBox.Left = 10
        chkBox.Top = 60 + ((i - 1) * 20)
        i = i + 1
    Next ws
    Application.ScreenUpdating = True
End Sub

Private Sub UserForm_Activate()
    CheckSize
End Sub

Private Sub CheckSize()
    Dim h, w
    Dim c As Control
    h = 0: w = 0
    For Each c In Me.Controls
        If c.Visible Then
            If c.Top + c.Height > h Then h = c.Top + c.Height
            If c.Left + c.Width > w Then w = c.Left + c.Width
        End If
    Next c
    If h > 0 And w > 0 Then
        With Me
            .Width = w + 40
            .Height = h + 40
        End With
    End If
End Sub
Next, do the following:
-in the Visual Basic Editor, click 'Insert' in the menu at the top
-click 'Module'
-copy/paste the macro below into the empty window that pops up
-press the F5 key to run the macro that will display the userform
(There are easier ways to run macros such as clicking a button or a letter of your choice on the keyboard. If you are interested in this approach, please let me know.)
-in the userform, place a check mark to the left of each of the six desired sheets and click the command button. The new workbook will be created.
VBA Code:
Sub ShowForm()
    UserForm1.Show
End Sub
Because I was unable to test this in your actual workbook, I'm not sure how it is going to work. As I mentioned earlier, it worked in my dummy file.
 
Upvote 0
Hi
Sorry for the late
Here's a sample file with the same sctruture:
2022Test1

I see now that you published a solution, I'm going to try, but please look to the file
 
Upvote 0
Hi
Thank you for the code above (#13).
However it may work (didn't test yet), I think it's not the way I need to. Your suggestion means to create a userform in each sheet in the existing file or in the new file?
The new file generated from selected sheets is to distribute to some partners and teams, so it's supposed to be ready and include filters, cells with no zeros, etc.
As shown in the sample, there are several sheetswith the same structure and one that is different
 
Upvote 0
You need only one user form.
 
Upvote 0
You need only one user form.
Hi
I created the userform in Workbook (the existing one)
Next, I added to original code "Call ShowForm" to activate the form to filter data.
Nothing happens:
The new file is created in the same structure and without the improvements I need
 
Upvote 0
Click here to download a sample file with dummy data. Click the button on the first sheet and select the desired sheets in the pop up. Most likely your actual sheets are not organized in the same fashion as those in this file and that is probably the cause of the problem.
 
Upvote 0
Hi
Code returns error '1004' - AutoFilter method of Range class failed
VBA Code:
            .Range("A1").CurrentRegion.AutoFilter 28, v, xlFilterValues
It cpoies the wnted sheets but doesn't finish the task
Also, in the sheet (PLAN) cells would be colored like the original (conditional formatting). I don't undrstand why it doesn't happen because other sheets return the colors in the new file
 
Upvote 0
Do you want to filter the PLAN sheet for any value?
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,495
Members
449,088
Latest member
Melvetica

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