Split workbook into worksheets

Chaverim

New Member
Joined
Nov 21, 2017
Messages
7
Hi,
I have got an Excel workbook with a few sheets in. I would like to split the workbook so that each separate sheet is transferred to a new workbook using VBA (in other words, split a workbook into many workbooks) and I would like to be able to choose where to save the new workbooks when the macro is running (meaning, open the save as dialogue).

Please advise me what to do.

Many thanks.
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
paste this code into a module (developer vbe area, insert, modules)
then run macro: ExportAllSheets2Nam

Code:
'----------------
Public Sub ExportAllSheets2Name()
'----------------
Dim sht, vFile, vDir

vDir = getMyDocs()
For Each sht In Worksheets
     'MsgBox sht.Name
    vFile = UserFileSaveAs(vDir, sht.Name)
    If vFile = "" Then Exit Sub
    vDir = getDirName(vFile)
    
    sht.Copy
    ActiveWorkbook.SaveAs Filename:=vFile, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Close False
    
Next
End Sub

'----------------
Public Function UserFileSaveAs(ByVal pvPath, ByVal pvName)
'----------------
Dim strTable As String
Dim strFilePath As String
Dim sDialog As String, sDecr  As String, sExt As String


                                'MUST ADD REFERENCE : Microsoft Office 11.0 Object Library
With Application.FileDialog(msoFileDialogSaveAs)
'With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Title = "Save sheet:" & UCase(pvName)
    .ButtonName = "Save"
    '.Filters.Clear
    '.Filters.Add "All Files", "*.*"
    .InitialFileName = pvPath
    .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
    ''.AllowMultiSelect = True
     
        If .Show = 0 Then
           'There is a problem
           Exit Function
        End If
    
    'Save the first file selected
    UserFileSaveAs = Trim(.SelectedItems(1))
End With
End Function


'----------------
Public Function getMyDocs()
'----------------
Dim vDir, vUsr

On Error GoTo errDocs
vUsr = Environ("UserProfile")
vDir = vUsr & "\Documents\"
If Not DirExists(vDir) Then
    vDir = vUsr & "\My Documents\"
    If Not DirExists(vDir) Then
       vDir = "c:\temp"
       MakeDir vDir
    End If
End If
getMyDocs = vDir
Exit Function
errDocs:
MsgBox "Cannot find temp folder", vbInformation, "getMyDocs():" & Err
End Function

'----------------
Public Function getDirName(ByVal psFilePath)
'----------------
Dim i As Integer, sDir As String

i = InStrRev(psFilePath, "\")
If i > 0 Then getDirName = Left(psFilePath, i)
End Function
 
Last edited:
Upvote 0
Thanks. The code didn't work, it said that there was a problem with [DirExist] within the Public Function getMyDocs!
 
Upvote 0
oops, i forgot it, sorry:

Code:
Public Function DirExists(ByVal pvDir) As Boolean
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
DirExists = FSO.FolderExists(pvDir)
Set FSO = Nothing
End Function
 
Upvote 0
Hi Chaverim

As you can see from reactions to your cross-post here, cross-posting isn't exactly good netiquette. That's because members get frustrated when they make contributions without knowing that you are already enlisting help elsewhere, and possibly have a solution to the problem.

If you are going to cross-post, please ensure that you post the links back to the cross-posts in your thread here so that members may see what suggestion have already been offered.
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,212
Members
449,090
Latest member
bes000

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