Nando1988

New Member
Joined
Aug 21, 2019
Messages
20
I want to export all sheets as xlsx with vba, and save that macro in the personal workbook.
I searched online for some code and then I modified it, so that I could select the folder in which I want to save the sheets in, and it works, but only if the macro is saved in the workbook I am working on, but it no longer works, if I save it in the personal workbook.
This is the code I have:
Code:
Sub SplitWorkbook()
'Updateby20140612
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim xWs As Worksheet
Dim xWb As Workbook
Dim FolderName As String
Dim selectedfolder As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show() = -1 Then
        selectedfolder = .SelectedItems(1)
        Application.ScreenUpdating = False
        Set xWb = Application.ThisWorkbook
        DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
        FolderName = selectedfolder & "\" & xWb.Name & " " & DateString
        MkDir FolderName
        For Each xWs In xWb.Worksheets
            xWs.Copy
            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
            xFile = FolderName & "\" & Application.ActiveWorkbook.Sheets(1).Name & FileExtStr
            Application.ActiveWorkbook.SaveAs xFile, FileFormat:=FileFormatNum
            Application.ActiveWorkbook.Close False
        Next
        MsgBox "You can find the files in " & FolderName
        Application.ScreenUpdating = True
Else
End If
End With
End Sub
Any help to fix the issue with this macro and the personal workbook will be greatly appreciated.
Thanks in advance.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello,

You can test following modification

Set xWb = ActiveWorkbook

Hope this will help
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,038
Members
448,940
Latest member
mdusw

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