Save multiple sheets that contain name "sheet...." as individual workbooks - excel

How do I save a specific sheet to a new workbook using Excel VBA?
I have multiple sheets with names "Sheet1", "Sheet2", "Sheet3" and so on.
I'd like to save all, in individual workbooks, with a single click.
This is returns an alert
Method Save as of object workbook failed
Sub SaveSplitSheet()
Dim ws As Worksheet
Dim wb As Workbook
For Each ws In ThisWorkbook.Sheets
If ws.Name Like "Sheet" & "*" Then
Application.DisplayAlerts = False
ws.Copy
ActiveWorkbook.SaveAs "/Users/Tukiyem/Downloads", FileFormat:=56
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
End If
Next
End Sub
Found the answer-> the code below saves multiple sheets that contain name "sheet...." as individual workbooks.
Sub SaveAsInLoop()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.Name Like "Sheet" & "*" Then
Application.DisplayAlerts = False
ws.Copy
ActiveWorkbook.SaveAs "/Users/Tukiyem/Downloads/" & ws.Name & ".xlsx", FileFormat:=51
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
End If
Next
End Sub

I would slightly tweak your code to a For...Next loop rather For Each...Next which will allow the evaluation of which number sheet we are up to in the loop.
This code is an example of how to loop through the worksheets. It will print each sheet name to the Immediate window of the VBE.
Just adapt your SaveAs code within the loop.
Sub SaveAsInLoop()
Dim SheetNumber As Long
For SheetNumber = 1 To ThisWorkbook.Sheets.Count
Debug.Print Sheets("Sheet" & SheetNumber).Name
Next SheetNumber
End Sub

Related

How to Split worksheets in excel file using VBA, with each new worksheet having an additional drop down list tab

Sub SplitEachWorksheet()
Dim FPath As String
FPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Sheets
ws.Copy
Application.ActiveWorkbook.SaveAs Filename:=FPath & "\" & ws.Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
I was using a VBA code that splits each worksheet into separate files (see above) however the problem is all the worksheets in the original file rely on one worksheet that have dropdown list values. (ie. if the worksheets were: monday, tuesday, wednesday, thursday, friday, dropdown lists), so by using the below vba code the dropdowns for monday through fridays worksheets are not working. How can I alter this code so that a copy of the dropdown worksheet/tab carries over with each worksheet? Or is there another solutions so that I can keep the dropdown list values in each tab and be able to split the file?
this code only separates each worksheet individually, but I need each worksheet in the file to split with a copy of a dropdown list tab that is found in the original file
Export Worksheets With Additional Same Worksheet
Option Explicit
Sub ExportWorksheets()
Const CopyWithAll As String = "DropDown Lists"
Dim DoNotCopy() As Variant: DoNotCopy = Array(CopyWithAll) ' add more!?
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim FolderPath As String: FolderPath = wb.Path & Application.PathSeparator
Application.ScreenUpdating = False
Dim ws As Worksheet
Dim wsName As String
For Each ws In wb.Worksheets
wsName = ws.Name
If IsError(Application.Match(wsName, DoNotCopy, 0)) Then
wb.Worksheets(Array(wsName, CopyWithAll)).Copy
With Workbooks(Workbooks.Count)
Application.DisplayAlerts = False ' overwrite, no confirmation
.SaveAs FolderPath & wsName
Application.DisplayAlerts = True
.Close False
End With
End If
Next ws
Application.ScreenUpdating = True
MsgBox "Worksheets exported.", vbInformation
End Sub

Save SPREADSHEETS in multiple excel files with the name of the main file

I have an Excel file with 4 auxaliary sheets + 7 sheets with tables.
I would like to copy and separate each sheet (of the 7 sheets) into multiple excel's, so that each excel file has only 1 table. These sheets starts with "Lista", as for example "Lista_AA", "Lista_BB"...
After I would like to save these sheets with same name they had in the main excel.
I don't have code because I try with with macro recorder and didn't function.I have already looked for several videos and questions on this site and they are a little different from what I want
I have this code for create these sheets in pdf:
Sub excels()
Application.ScreenUpdating = False
Dim i As Integer
Dim nome_arquivo As String
For i = 5 To Sheets.Count
nome_arquivo = Sheets(i).Name
With Sheets(i)
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & nome_arquivo & ".pdf"
End With
Next i
Application.ScreenUpdating = True
End Sub
Is it possible to adapt for Excel files for same sheets?
Use a loop:
Const filepath As String = "https://agits-my.sharepoint.com/personal/Documents/Desktop/Cantina/"
Sub macro()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "Lista_*" Then
SaveCopy ws:=ws
End If
Next
End Sub
Private Sub SaveCopy(ByVal ws As Worksheet)
ws.Copy
Dim wb As Workbook
Set wb = ActiveWorkbook
wb.SaveAs FileName:=filepath & ws.Name & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook, _
CreateBackup:=False
wb.Close SaveChanges:=False
End Sub

Save all worksheets as csvs and keep original file active

I have a question, I would like to save all my worksheets to seperate csv files and after it is finished the original excel file should be used. I've found some questions related to this on stackoverflow BUT I cannot combine them to make it work :(
Here is what I have at the moment which works:
Sub SaveSheetsAsCsv()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
ws.SaveAs ActiveWorkbook.Path & "\" & ws.Name & ".csv", xlCSV, Local:=True
Next
Application.DisplayAlerts = True
End Sub
The result, I have all my worksheets saved to the same folder BUT then my workbook is named as my last worksheet and if I want to close it says I have to save it ... but instead I would like to have my original excel file active.
Any idea how can I do that?
I've tried to implement this: Keep the same excel but I always get an error :(
Any advice and help would be appreciated.
The adjustment below copies the sheet to a new book, saves it as CSV and closes.
Sub SaveSheetsAsCsv()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
ws.Copy
ActiveWorkbook.SaveAs wb.Path & "\" & ws.Name & ".csv", xlCSV, Local:=True
ActiveWorkbook.Close
Next
Application.DisplayAlerts = True
End Sub

VBA. Export only visible sheet to individual workbook

Sub SaveShtsAsBook()
‘Select all visible and hide sheet’
Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
MyFilePath$ = ActiveWorkbook.Path & "\" & _
Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
With Application
.ScreenUpdating = False
.DisplayAlerts = False
' End With
On Error Resume Next '<< a folder exists
MkDir MyFilePath '<< create a folder
For N = 1 To Sheets.Count
Sheets(N).Activate
SheetName = ActiveSheet.Name
Cells.Copy
Workbooks.Add (xlWBATWorksheet)
With ActiveWorkbook
With .ActiveSheet
.Paste
.Name = SheetName
[A1].Select
End With
'save book in this folder
.SaveAs Filename:=MyFilePath _
& "\" & SheetName & ".xlsx"
.Close SaveChanges:=True
End With
.CutCopyMode = False
Next
End With
Sheet1.Activate
End Sub
I have a workbook, that contains many sheets which have visible and hide ones. I only want to export each visible sheet to individual workbook. this current code above can do the export for all the sheet in the workbook but I have to delete them 1 by 1 after that. Hope that explains my situation.
All you need to add to your code to exclude hidden sheets is a simple If..Then statement to check whether the Worksheet.Visible property is True or False.
If Not yourWorsheet.Visible Then... ... then you skip that worksheet.
The following procedure is a simpler overall approach to what you're trying to accomplish...
Export Visible worksheets to their own workbooks:
The worksheet.Copy method will create a new workbook if neither Before nor After are specified.
Sub saveVisibleSheetsAsXLSM() 'saves all visible sheets as new xlsx files
Const exportPath = "x:\yourDestinationPath\"
Dim ws As Worksheet, wbNew As Workbook
For Each ws In ThisWorkbook.Sheets 'for each worksheet
If ws.Visible Then 'if it's visible:
Debug.Print "Exporting: " & ws.Name
ws.Copy '(if no params specified, COPY creates + activates a new wb)
Set wbNew = Application.ActiveWorkbook 'get new wb object
wbNew.SaveAs exportPath & ws.Name & ".xlsm", 52 'save new wb
wbNew.Close 'close new wb
Set wbNew = Nothing 'cleanup
End If
Next ws
Set ws = Nothing 'clean up
End Sub
Worksheet.Copy Remarks:
If you don't specify either Before or After, Microsoft Excel creates a new workbook that contains the copied sheet object that contains the copied Worksheet object. The newly created workbook holds the Application.ActiveWorkbook Property (Excel) property and contains a single worksheet. The single worksheet retains the Worksheet.Name Property (Excel) and Worksheet.CodeName Property (Excel) properties of the source worksheet. If the copied worksheet held a worksheet code sheet in a VBA project, that is also carried into the new workbook.
An array selection of multiple worksheets can be copied to a new blank Workbook Object (Excel) object in a similar manner.
(Source: Documentation)

Export multiple worksheets to CSV files in a specified directory

I'm trying to do the following:
Export/Copy particular sheets in the workbook (any sheet name that contains "Upload") to a particular file directory.
I don't want these worksheet names to change nor the workbook name to change.
The file-name is consistent for each worksheet, so it would be okay to replace the files in the directory whenever I run the macro. It is okay to have a dialog box that asks if I'm sure I want to replace each of the files.
I don't want the newly created CSVs or any other file to open.
Sub SheetsToCSV()
'Jerry Beaucaire (1/25/2010), updated (8/15/2015)
'Save each sheet to an individual CSV file
Dim ws As Worksheet, fPATH As String
Application.ScreenUpdating = False 'speed up macro
Application.DisplayAlerts = False 'automatically overwrite old files
fPATH = "C:\2015\CSV\" 'path to save into, remember the final \ in this string
For Each ws In Worksheets
ws.Copy
ActiveWorkbook.SaveAs Filename:=fPATH & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close
Next ws
Application.ScreenUpdating = True
End Sub
You just need to add a simple loop through all worksheets and test the name.
Try this:-
Sub COPYSelectedSheetsToCSV()
Dim ws As Worksheet
'In case something goes wrong
On Error GoTo COPYSelectedSheetsToCSVZ
'Loop through all worksheets
For Each ws In ActiveWorkbook.Sheets
'Does the name contain "Upload"
If InStr(1, ws.Name, "Upload") > 0 Then
'Make the worksheet active
ws.Select
'Save it to CSV
ActiveWorkbook.SaveAs Filename:="/Users/reginaho/Desktop/Upload/" & ws.Name & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False
End If
Next
COPYSelectedSheetsToCSVX:
'Clean up the memory usage
Set ws = Nothing
Exit Sub
COPYSelectedSheetsToCSVZ:
MsgBox Err.Number & " - " & Err.Description
Resume COPYSelectedSheetsToCSVX
End Sub

Resources