VB creates workbooks for each sheet, but incorrect folder

AngelK

New Member
Joined
Aug 4, 2016
Messages
34
I have a workbook that has 20 sheets. My goal is to save each sheet to it's own workbook, in the same folder as my original workbook. The code below is in my Personal.xlsb, and it creates a workbook just for my vb book (although I have the 20-sheet workbook open), and saves it in my xlStart file. Not what I need.
If I copy the code to my ThisWorkbook, and hard code the file path, it works great (creates 20 workbooks in the right folder). I need to have this code create the folder and copy the workbooks where the original file is saved, not my personal.xlsb. Help!

Code:
Sub Copy_Every_Sheet_To_New_Workbook()
 
    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-2013
                    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 can find the files in " & FolderName
 
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
    End With
End Sub
 
Last edited:
Based on your script it is basing the file type of the destinations on the the Sourcewb, which you define as the activeworkbook. If your active workbook does not have macro's your script is told to save it as xlsx.

If you just want it forced just remove the logic for determining the source type and set the save as type to xlsm
 
Upvote 0
This means that in point 2 of the macro logic (see post#9) the line "- Sheet1.xlsx without macro" is not correct.
Then replace this part:
Rich (BB code):

              Case 52, 56:
                If .HasVBProject Then
                  FileExtStr = ".xlsm": FileFormatNum = 52
                Else
                  FileExtStr = ".xlsx": FileFormatNum = 51
                End If

By that one:
Rich (BB code):

              Case 52, 56:
                FileExtStr = ".xlsm": FileFormatNum = 52
But please take into account that macro of the source workbook, which are outside the sheets (for example in Modiule1), are not copied at all by your code
 
Last edited:
Upvote 0
That makes sense. Thank you. This change of code did the job.

Also, I inserted the code to rename the new workbook sheet1 to "Teacher", but it's not working. The sheet1 of the new workbook is still the teacher's name, not "Teacher":

Code:
   'Determine the Excel version and file extension/format
            With Destwb
            
                 ' The 1st saved sheet is renamed to "Teacher"
                If .Sheets(1).name = Sourcewb.Sheets(1).name Then .Sheets(1).name = "Teacher"
                
                If Val(Application.Version) < 12 Then
                    'You use Excel 97-2003
                    FileExtStr = ".xls": FileFormatNum = -4143
                Else
                    'You use Excel 2007-2013
                    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, 56:
                        FileExtStr = ".xlsm": FileFormatNum = 52
                        Case 56: FileExtStr = ".xls": FileFormatNum = 56
                        Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                        End Select
                    End If
                End If

Do I have it in the correct place?

Thank you for your help!
 
Upvote 0
Also, I inserted the code to rename the new workbook sheet1 to "Teacher", but it's not working. The sheet1 of the new workbook is still the teacher's name, not "Teacher":
Rich (BB code):
   'Determine the Excel version and file extension/format
            With Destwb
            
                 ' The 1st saved sheet is renamed to "Teacher"
                 If .Sheets(1).name = Sourcewb.Sheets(1).name Then .Sheets(1).name = "Teacher"

Do I have it in the correct place?
Yes it's the correct place. Do you see Teacher.xlsm in the folder?
Sheets(1) is used in the code there 1 is the index of the sheet
Excel consider the 1st sheet (actually with its index = 1) according to the order the sheets were inserted into workbook regardless its tab is on the leftmost side or not.
To find the correct index of the sheet use this test code:
Rich (BB code):
Sub Test()
  Dim sh As Worksheet
  For Each sh In ActiveWorkbook.Sheets
    Debug.Print "Index=" & sh.Index, "Name=" & sh.Name, "CodeName=" & sh.CodeName
  Next
End Sub
If CodeName is the same for teachers then CodeName can be used instead of Sheets(1)
 
Last edited:
Upvote 0
Thank you. I ran the above code, but it didn't do anything. The original code (#1) creates a new workbook for every sheet (teacher) in my "all teachers" workbook. The new "one teacher" workbook created only contains one sheet, which is the teacher's name. There are no other sheets in the new workbook.

I'm currently getting around this just by renaming the sheet (teacher's name) to "Teacher", just wanted to save the end user this step. I can live with it, but thought it was worth asking.
Thanks for everything!
 
Upvote 0
Thank you. I ran the above code, but it didn't do anything. The original code (#1) creates a new workbook for every sheet (teacher) in my "all teachers" workbook. The new "one teacher" workbook created only contains one sheet, which is the teacher's name. There are no other sheets in the new workbook.

I'm currently getting around this just by renaming the sheet (teacher's name) to "Teacher", just wanted to save the end user this step. I can live with it, but thought it was worth asking.
Thanks for everything!
The output of code Test from post #14 is in the Immediate window of VBE: run Test macro, press Alt-F11 and then press Ctrl-G to go into the Immediate window.
Glad you've got the solution! Post back if you get any problems
 
Upvote 0

Forum statistics

Threads
1,226,798
Messages
6,193,065
Members
453,773
Latest member
bclever07

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