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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
If there is no data in column AB to use a filter criteria, do you want to exclude that sheet?
 
Upvote 0
If there is no data in column AB to use a filter criteria, do you want to exclude that sheet?
I want to copy that sheet too, as shown in the original, only values and formats.
actually the code copies the sheet with values only, but I want the colors in the cells. Code do that in the other sheets. That one is a different layout, is it possible?
 
Upvote 0
Click here to download your file. This is the modified code:
VBA Code:
Private Sub CommandButton1_Click()
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    Dim c As MSForms.Control, shArr() As Variant, n As Long, ws As Worksheet, v As Variant, rng As Range, srcWB As Workbook
    Set srcWB = ThisWorkbook
    v = Array("YES", "NO", "WAIT")
    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
        If ws.Name <> "PLAN" Then
            If WorksheetFunction.CountA(srcWB.Sheets(ws.Name).Range("AB:AB")) > 1 Then
                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
            End If
        Else
            With ws.UsedRange.Cells
                .Value = .Value
                .Replace 0, "", xlWhole
            End With
        End If
    Next ws
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, chkBox As Control
    For Each ws In Sheets
        If ws.Name <> "GenerateFile" Then
            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
        End If
    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
Run the macro in Module2. Be patient when running the code. Please note that I deleted the first row in the first sheet.
 
Upvote 0
Hi Mumps
Thank you for your patience and time. I'm feeling so bad to insist.
The code works fine and quick, but...
Still the sheet "PLAN" - colors don't show up in cells like the original, cells still remain all white. is it possible to make that correction?

In addition, first code i've published include a save as for the new workbook, would you please add/adapt those lines?

Tkanks
 
Upvote 0
This is a copy of the PLAN sheet after I run the macro. As you can see, all the colours are there so I don't know why the colours don't show up when you run it.
JonasTigervalues.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXY
1ABCDEFGYESNOWAITSTILL
2
3
4AML
5#NAME?#NAME?#NAME?#NAME?#NAME?
6
72022-01-242022-01-252022-01-262022-01-272022-01-282022-01-312022-02-012022-02-022022-02-032022-02-042022-02-072022-02-082022-02-092022-02-102022-02-112022-02-142022-02-152022-02-162022-02-172022-02-182022-02-212022-02-222022-02-232022-02-24
8322428385167720614465450256516
9321427384121719613299176306
1015942638374280612170114296
1142521073279340113
12424209177
13423208140
14422
15
16
17
18
19AMP
20#NAME?#NAME?#NAME?#NAME?#NAME?
21
222022-01-242022-01-252022-01-262022-01-272022-01-282022-01-312022-02-012022-02-022022-02-032022-02-042022-02-072022-02-082022-02-092022-02-102022-02-112022-02-142022-02-152022-02-162022-02-172022-02-182022-02-212022-02-222022-02-232022-02-24
2311791249141914761282
2410031110108114751121
25994110914741073
26110810341072
2710151033970
281032931
29
30
31
32
33
34NORUP
35#NAME?#NAME?#NAME?#NAME?#NAME?
36
372022-01-242022-01-252022-01-262022-01-272022-01-282022-01-312022-02-012022-02-022022-02-032022-02-042022-02-072022-02-082022-02-092022-02-102022-02-112022-02-142022-02-152022-02-162022-02-172022-02-182022-02-212022-02-222022-02-232022-02-24
3823462451246924782450
3922582352246824772385
4022572301227524762315
412256224822742475
4222552474
432473
44
45
46
47
48
49CEMID
50#NAME?#NAME?#NAME?#NAME?#NAME?
51
522022-01-242022-01-252022-01-262022-01-272022-01-282022-01-312022-02-012022-02-022022-02-032022-02-042022-02-072022-02-082022-02-092022-02-102022-02-112022-02-142022-02-152022-02-162022-02-172022-02-182022-02-212022-02-222022-02-232022-02-24
531891186418662110152719131917
5417391695186515261912
5517381694174215251911
5615851693156914971910
571555164414961909
581533164314951908
5914941907
601906
611905
62
63
64SOUDOWN
65#NAME?#NAME?#NAME?#NAME?#NAME?
66
672022-01-242022-01-252022-01-262022-01-272022-01-282022-01-312022-02-012022-02-022022-02-032022-02-042022-02-072022-02-082022-02-092022-02-102022-02-112022-02-142022-02-152022-02-162022-02-172022-02-182022-02-212022-02-222022-02-232022-02-24
682870286127282836
69275527272835
70269427262834
71269327252833
72269227012832
73269127002831
742738
PLAN


Try this revised macro:
VBA Code:
Private Sub CommandButton1_Click()
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    Dim c As MSForms.Control, shArr() As Variant, n As Long, ws As Worksheet, v As Variant
    Dim rng As Range, srcWB As Workbook, p As Long, newXlsxFullName As String
    Set srcWB = ThisWorkbook
    p = InStrRev(srcWB.FullName, ".")
    newXlsxFullName = Left(srcWB.FullName, p - 1) & "values.xlsx"
    v = Array("YES", "NO", "WAIT")
    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
        If ws.Name <> "PLAN" Then
            If WorksheetFunction.CountA(srcWB.Sheets(ws.Name).Range("AB:AB")) > 1 Then
                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
            End If
        Else
            With ws.UsedRange.Cells
                .Value = .Value
                .Replace 0, "", xlWhole
            End With
        End If
    Next ws
    On Error Resume Next
    With ActiveWorkbook
        Application.DisplayAlerts = False
        .SaveAs newXlsxFullName, FileFormat:=xlOpenXMLWorkbook
        Application.DisplayAlerts = True
        .Close SaveChanges:=False
        If Err.Number = 0 Then
            MsgBox "Saved " & newXlsxFullName, vbInformation
        Else
            MsgBox "Error saving " & newXlsxFullName & vbCrLf & vbCrLf & "Error number " & Err.Number & vbCrLf & Err.Description, vbExclamation
        End If
    End With
    On Error GoTo 0
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    Unload Me
End Sub
 
Upvote 0
I wish I understand too
You´ve done a great job, Tank you very much.
Everything works very fine except that "thing".
I'm testing in the same file I sent you and "bug" still remains:

imgTest1.JPG


Is this about EXCEL version? I'm working v2019 and 365
 
Upvote 0
I'm not sure what the problem is because the code simply copies the whole sheet as is.
 
Upvote 0
I'm not sure what the problem is because the code simply copies the whole sheet as is.
I think there's a bug for sure, I tested in original file and in two more testfiles, all of them with the same layout, and result is always diferent in this sheet "PLAN" - some with values, some with blanks, but none with colored cells.
Another thing that happens is Excel ShutDown, everytime the macro runs
 
Upvote 0
I'm sorry but I can't seem to reproduce the problem.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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