VBA. Save sheets as values in separate workbooks. The problem is, all data in original file is saved as metadata.

Gloffo

New Member
Joined
Sep 16, 2022
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Background:
I have a file containing sensitive information. I process that information and assign each row to different units, where some information goes to several units. Everything is compiled in a pivot table, and then I have a macro that goes through several different cycles of "Show report filter pages", so in the end I end up with around a 100 worksheets, each containing a pivot table with only the information that particular unit should receive. Then I run a VBA-script, that I have no idea who wrote in the first place, but copies and versions of it can be found in every forum on the internet in how to save each worksheet as a separate file.

I started doing this when my organisation had Excel 2010, and all worked extremely well. A colleague of mine took over this routine. We then changed version, and now run Excel 2019, and this was when he encountered some problems. The whole procedure all of a sudden took ages to run. He has now adapted to this and just start it at the end of the day, and let it run for an hour or so. I just looked at it and noticed that the new files are huge. When using Excel 2010 the new saved files were 50-200 kB, but now they are about 5000 kB. If I open the file, it looks like it only contains one sheet and everything is only values, but if I change the file extension to .zip, and unpack and look at the metadata I can see that all the data that make up the original pivot table is hidden in each file. This creates two major problems; the files are so big that they overcrowd the mailboxes, and the worst thing is that we actually mail sensitive information that is not supposed to be shared to each recipient.

I have no idea why Excel save the files differently between 2010 and 2019. Some help on how to get around this would be much appreciated.

This is tha VBA script I use (thank you original creator).
VBA Code:
Sub Copy_Every_Sheet_To_New_Workbook()

Application.ScreenUpdating = False

'Working in 97-2010

    Dim FileExtStr As String

    Dim FileFormatNum As Long

    Dim Sourcewb As Workbook

    Dim Destwb As Workbook

    Dim sh As Worksheet

    Dim DateString As String

    Dim FolderName As String

 

    With Application

        .ScreenUpdating = False

        .EnableEvents = False

        .Calculation = xlCalculationManual

    End With

 

    'Copy every sheet from the workbook with this macro

    Set Sourcewb = ThisWorkbook

 

    'Create new folder to save the new files in

    DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")

    FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString

    MkDir FolderName

 

    'Copy every visible sheet to a new workbook

    For Each sh In Sourcewb.Worksheets

 

        'If the sheet is visible then copy it to a new workbook

        If sh.Visible = -1 Then

            sh.Copy

 

            'Set Destwb to the new workbook

            Set Destwb = ActiveWorkbook

 

            'Determine the Excel version and file extension/format

            With Destwb

                If Val(Application.Version) < 12 Then

                    'You use Excel 97-2003

                    FileExtStr = ".xls": FileFormatNum = -4143

                Else

                    'You use Excel 2007-2010

                    If Sourcewb.Name = .Name Then

                        MsgBox "Your answer is NO in the security dialog"

                        GoTo GoToNextSheet

                    Else

                        Select Case Sourcewb.FileFormat

                        Case 51: FileExtStr = ".xlsx": FileFormatNum = 51

                        Case 52:

                            If .HasVBProject Then

                                FileExtStr = ".xlsm": FileFormatNum = 52

                            Else

                                FileExtStr = ".xlsx": FileFormatNum = 51

                            End If

                        Case 56: FileExtStr = ".xls": FileFormatNum = 56

                        Case Else: FileExtStr = ".xlsb": FileFormatNum = 50

                        End Select

                    End If

                End If

            End With

 

            'Change all cells in the worksheet to values if you want

            If Destwb.Sheets(1).ProtectContents = False Then

                With Destwb.Sheets(1).UsedRange

                    .Cells.Copy

                    .Cells.PasteSpecial xlPasteValues

                    .Cells(1).Select

                End With

                Application.CutCopyMode = False

            End If

 

 

            'Save the new workbook and close it

            With Destwb

                .SaveAs FolderName & "\" & Destwb.Sheets(1).Name & FileExtStr, FileFormat:=FileFormatNum

                .Close False

            End With

 

        End If

GoToNextSheet:

    Next sh

 

    MsgBox "You find your files in " & FolderName

 

    With Application

        .ScreenUpdating = True

        .EnableEvents = True

        .Calculation = xlCalculationAutomatic

    End With
    Application.ScreenUpdating = True
    
    MsgBox "Files are created"
    
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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