Forum Discussion

Pinherio's avatar
Pinherio
Copper Contributor
Feb 23, 2024
Solved

Split sheets into specific location with named folder

Hello, 

 

I am using this code which splits all excel sheets into specific location, I just want to split one sheet not all of them. what should I modify?

 

Sub SplitWorkbook()

Dim FileExtStr As String
Dim FileFormatNum As Long
Dim xWs As Worksheet
Dim xWb As Workbook
Dim xNWb As Workbook
Dim FolderName As String
Dim Path2 As String
Application.ScreenUpdating = False
Set xWb = Application.ThisWorkbook
Path2 = "C:\Users\**********\Desktop\********"
DateString = Format(Now, "dd.mm.yyyy hh.mm.ss")
FolderName = Path2 & "\" & Sheets("******").Range("B2").Value & " " & Sheets("Overview").Range("J3").Value & " " & DateString

If Val(Application.Version) < 12 Then
FileExtStr = ".xls": FileFormatNum = -4143
Else
Select Case xWb.FileFormat
Case 51:
FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If Application.ActiveWorkbook.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

MkDir FolderName

For Each xWs In xWb.Worksheets
On Error GoTo NErro
If xWs.Visible = xlSheetVisible Then
xWs.Select
xWs.Copy
xFile = FolderName & "\" & xWs.Name & FileExtStr
Set xNWb = Application.Workbooks.Item(Application.Workbooks.Count)
xNWb.SaveAs xFile, FileFormat:=FileFormatNum
xNWb.Close False, xFile
End If
NErro:
xWb.Activate
Next

MsgBox "The process is done"
Application.ScreenUpdating = True

End Sub

 

Thanks in advance

  • Pinherio 

    Try this version:

    Sub SplitWorkbook()
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim xWs As Worksheet
        Dim xWb As Workbook
        Dim xNWb As Workbook
        Dim FolderName As String
        Dim Path2 As String
        Dim DateString As String
        Dim xFile As String
        Application.ScreenUpdating = False
        Set xWb = Application.ThisWorkbook
        ' *** Use only one of the following
        Set xWs = xWb.Worksheets(1) ' to copy the 1st sheet
        ' or
        Set xWs = ActiveSheet ' to copy the currently active sheet
        ' ***
        Path2 = "C:\Users\**********\Desktop\********"
        DateString = Format(Now, "dd.mm.yyyy hh.mm.ss")
        FolderName = Path2 & "\" & Sheets("******").Range("B2").Value & " " & _
            Sheets("Overview").Range("J3").Value & " " & DateString
        If Val(Application.Version) < 12 Then
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            Select Case xWb.FileFormat
                Case 51:
                    FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If Application.ActiveWorkbook.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
        MkDir FolderName
        On Error GoTo NErro
        xWs.Copy
        xFile = FolderName & "\" & xWs.Name & FileExtStr
        Set xNWb = Application.Workbooks.Item(Application.Workbooks.Count)
        xNWb.SaveAs xFile, FileFormat:=FileFormatNum
        xNWb.Close False
    NErro:
        xWb.Activate
        MsgBox "The process is done"
        Application.ScreenUpdating = True
    End Sub

2 Replies

  • Pinherio 

    Try this version:

    Sub SplitWorkbook()
        Dim FileExtStr As String
        Dim FileFormatNum As Long
        Dim xWs As Worksheet
        Dim xWb As Workbook
        Dim xNWb As Workbook
        Dim FolderName As String
        Dim Path2 As String
        Dim DateString As String
        Dim xFile As String
        Application.ScreenUpdating = False
        Set xWb = Application.ThisWorkbook
        ' *** Use only one of the following
        Set xWs = xWb.Worksheets(1) ' to copy the 1st sheet
        ' or
        Set xWs = ActiveSheet ' to copy the currently active sheet
        ' ***
        Path2 = "C:\Users\**********\Desktop\********"
        DateString = Format(Now, "dd.mm.yyyy hh.mm.ss")
        FolderName = Path2 & "\" & Sheets("******").Range("B2").Value & " " & _
            Sheets("Overview").Range("J3").Value & " " & DateString
        If Val(Application.Version) < 12 Then
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            Select Case xWb.FileFormat
                Case 51:
                    FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If Application.ActiveWorkbook.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
        MkDir FolderName
        On Error GoTo NErro
        xWs.Copy
        xFile = FolderName & "\" & xWs.Name & FileExtStr
        Set xNWb = Application.Workbooks.Item(Application.Workbooks.Count)
        xNWb.SaveAs xFile, FileFormat:=FileFormatNum
        xNWb.Close False
    NErro:
        xWb.Activate
        MsgBox "The process is done"
        Application.ScreenUpdating = True
    End Sub