Save each worksheet with formatting in loop - excel

I have a template file and 4 source documents that I use to fill the template. For each row in sheet2, I create a new blank template and fill it out, resulting in somewhere between 10-100 files. I want to save these in a loop, but having issues with Excel force closing on me. This is my code so far, recycled from a different project.
Dim w As Long, wb As Workbook
Dim fp As String, fn As String
Dim folderName As String
Dim fsoFSO
On Error GoTo bm_Safe_Exit
'Application.ScreenUpdating = False 'stop screen flashing
Application.DisplayAlerts = False 'stop confirmation alerts
'start with a reference to ThisWorkbook
With ThisWorkbook
folderName = Format(Date, "ddmmyyyy")
'set path to save
'fp = "<PATH HERE>" & folderName
fp = "C:\Users\Username\OneDrive - CompanyName\Documents\Projects\ThisProject\csvOutput\" & folderName
Set fsoFSO = CreateObject("Scripting.FileSystemObject")
If fsoFSO.FolderExists(fp) Then
MsgBox "FOLDER - " & fp & " ALREADY EXISTS, DELETE CONTENTS TO PROCEED"
Exit Sub
Else
fsoFSO.CreateFolder (fp)
End If
'cycle through each of the worksheets
For w = 6 To Worksheets.Count
With Worksheets(w)
.Copy
'the ActiveWorkbook is now the new workbook populated with a copy of the current worksheet
With ActiveWorkbook
fn = .Worksheets(1).Name
Cells.Select
Selection.SpecialCells(xlCellTypeVisible).Copy
Worksheets.Add after:=Sheets(Sheets.Count)
Range("A1").Select
ActiveSheet.Paste
Worksheets(1).Delete
Worksheets(1).Name = fn
.SaveAs Filename:=fp & Chr(92) & fn, FileFormat:=51
.Close savechanges:=False '<~~ already saved in line above
End With
End With
Next w
End With
bm_Safe_Exit:
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub ```

The code below worked for me: not sure exactly where the problem might vbe with your posted code, but within your With blocks not everything is scope to the block using a leading .
Sub Test()
Dim w As Long, wb As Workbook, wbNew As Workbook
Dim fp As String, fn As String
Dim fsoFSO
On Error GoTo bm_Safe_Exit
Set wb = ThisWorkbook
fp = "C:\Users\Username\OneDrive - CompanyName\Documents\Projects\" & _
"ThisProject\csvOutput\" & Format(Date, "ddmmyyyy") & "\"
Set fsoFSO = CreateObject("Scripting.FileSystemObject")
If fsoFSO.FolderExists(fp) Then
MsgBox "FOLDER - " & fp & " ALREADY EXISTS, DELETE CONTENTS TO PROCEED"
Exit Sub
Else
fsoFSO.CreateFolder fp
End If
'cycle through each of the worksheets
For w = 6 To wb.Worksheets.Count
'explicitly create a new single-sheet workbook as the destination
Set wbNew = Workbooks.Add(Template:=xlWBATWorksheet)
wb.Worksheets(w).Copy before:=wbNew.Sheets(1)
DeleteSheet wbNew.Sheets(2)
With wbNew
fn = .Worksheets(1).Name
.Worksheets.Add after:=.Worksheets(.Worksheets.Count)
.Worksheets(1).Cells.SpecialCells(xlCellTypeVisible).Copy _
Destination:=.Worksheets(2).Range("A1")
DeleteSheet .Worksheets(1)
.Worksheets(1).Name = fn
.SaveAs Filename:=fp & fn, FileFormat:=51
.Close savechanges:=False '<~~ already saved in line above
End With
Next w
Exit Sub
bm_Safe_Exit:
MsgBox Err.Description
End Sub
'utility sub
Sub DeleteSheet(ws As Worksheet)
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End Sub

Related

Save my WB based on another workbook name in VBA

I have a code which is doing following:
Prompt to choose external workbook
Copying all the data from that wb
Pasting exactly 1:1 in main wb
Close and Save from .xlsm to .xlsx but with a name of my main wb
Sub CopySheetFromClosedWorkbook2()
'Prompt to choose your file in the chosen locatioon
Dim dialogBox As FileDialog
Dim FilePath As String
Set dialogBox = Application.FileDialog(msoFileDialogOpen)
Application.StatusBar = "Choose older PDS Form!"
dialogBox.AllowMultiSelect = False
dialogBox.Title = "Select a file"
If dialogBox.Show = -1 Then
FilePath = dialogBox.SelectedItems(1)
'If nothing selected then MsgBox
Else
MsgBox "No PDS Form selected!"
Exit Sub
End If
'Here are sheets defined which you are going to copy/paste (reference update) but to keep formatting.
''Sheets should be defined from right to left to have your sheets sorted from the beginning
Dim shNames As Variant: shNames = Array("CH_or_Recipe_8", "CH_or_Recipe_7", "CH_or_Recipe_6", "CH_or_Recipe_5", "CH_or_Recipe_4", _
"CH_or_Recipe_3", "CH_or_Recipe_2", "CH_or_Recipe_1", "Customer Details", "Instructions")
Dim tgt As Workbook: Set tgt = ThisWorkbook
Application.ScreenUpdating = False
Dim src As Workbook: Set src = Workbooks.Open(FilePath)
Dim ws As Worksheet, rng As Range, i As Long
For Each ws In src.Sheets
If ws.Name Like "*[1-8]" Then
ws.Name = "CH_or_Recipe_" & Right(ws.Name, 1)
ElseIf ws.Name = "Customer_Details" Then
ws.Name = "Customer Details"
ElseIf ws.Name = "OIPT Plasmalab" Then
ws.Name = "CH_or_Recipe_1"
ElseIf ws.Name = "AMAT" Then
ws.Name = "CH_or_Recipe_2"
End If
Next
For i = 0 To UBound(shNames)
On Error Resume Next
Set ws = src.Sheets(shNames(i))
If Err.Number = 0 Then
tgt.Worksheets(shNames(i)).Cells.Clear
Set rng = ws.UsedRange
rng.Copy tgt.Worksheets(shNames(i)).Range(rng.Address)
End If
Next i
src.Close False
Application.ScreenUpdating = True
MsgBox "Copy&Paste successful!"
End Sub
Sub SaveNoMacro()
Dim fn As String
With ThisWorkbook
fn = Replace(.FullName, ".xlsm", ".xlsx")
Application.DisplayAlerts = False
.SaveAs fn, FileFormat:=xlWorkbookDefault
Application.DisplayAlerts = True
End With
MsgBox "Saved as " & fn
End Sub
What I just need (if possible) is to save my wb in the same name as that external wb that I am taking data from and adding date/time at the end.
Example:
MainWB1.xlsm + ExternalWB1.xlsx >>> MainWB1.xlsx (This is now)
MainWB1.xlsm + ExternalWB1.xlsx >>> ExternalWB1_today().xlsx (This is what I wanna)
You have 2 separate methods:
CopySheetFromClosedWorkbook2
SaveNoMacro
The name of the source workbook is only available in the scope of the CopySheetFromClosedWorkbook2 because that's where you open and close it. So, you have 2 options:
Save the main workbook before exiting the scope of the CopySheetFromClosedWorkbook2 method i.e. while the name of the source book is available
Save the name of the source book somewhere (global variable, named range, registy, custom xml part etc.) or even return it as a result (Function instead of Sub) so that you can call the SaveNoMacro method at a later stage
Save before exiting the scope
Here are 2 ways to do this:
Place your save code before the src.Close False line so that you can use the src.Name property i.e. combine the 2 methods into one. Not sure if you want to do this
Pass the name as an argument to the second method. In CopySheetFromClosedWorkbook2 replace this:
src.Close False
with this:
SaveNoMacro src.Name
src.Close False
and update SaveNoMacro to:
Sub SaveNoMacro(ByVal newName As String)
Dim fn As String
With ThisWorkbook
fn = Replace(.FullName, .Name, Left(newName, InStrRev(newName, ".") - 1)) _
& Format$(Date, "_yyyy-mm-dd") & ".xlsx"
Application.DisplayAlerts = False
.SaveAs fn, FileFormat:=xlWorkbookDefault
Application.DisplayAlerts = True
End With
MsgBox "Saved as " & fn
End Sub
Save the name for later use
In case you don't want to run the 2 methods in a sequence then you can save the name for later use. Using a global variable is not a good idea as the state can be lost by the time you run the save method. Using a named range would work as long as you don't have your workbook protected i.e you can create a named range.
There are many options but the easiest to use is to write to registry using the built in SaveSetting option. Replace this:
src.Close False
with this:
SaveSetting "MyApp", "MySection", "NewBookName", src.Name
src.Close False
and update SaveNoMacro to:
Sub SaveNoMacro()
Dim fn As String: fn = GetSetting("MyApp", "MySection", "NewBookName")
If LenB(fn) = 0 Then
MsgBox "No name was saved", vbInformation, "Cancelled"
Exit Sub
Else
DeleteSetting "MyApp", "MySection", "NewBookName"
End If
With ThisWorkbook
fn = Replace(.FullName, .Name, Left(fn, InStrRev(fn, ".") - 1)) _
& Format$(Date, "_yyyy-mm-dd") & ".xlsx"
Application.DisplayAlerts = False
.SaveAs fn, FileFormat:=xlWorkbookDefault
Application.DisplayAlerts = True
End With
MsgBox "Saved as " & fn
End Sub
fn = Replace(.FullName, ".xlsm", ".xlsx")
fn = Replace(.FullName, ".xlsm", date & ".xlsx")
What I just need (if possible) is to save my wb in the same name as that external wb that I am taking data from and adding date/time at the end
You got the full path of your external wb in the variable FilePath so you can use that to save the workbook. You could save it like this (at the end of your sub CopySheetFromClosedWorkbook2):
Dim SaveName As String
SaveName = src.Path & "\" & Replace(Split(Filepath, "\")(UBound(Split(Filepath, "\"))), ".xlsm", Format(Date, "dd_mm_yyyy") & ".xlsx")
With ThisWorkbook
Application.DisplayAlerts = False
.SaveAs SaveName, FileFormat:=xlWorkbookDefault
Application.DisplayAlerts = True
End With
Notice I'm using the object src to get the path where you want to save the new workbook, so you need to asign the line SaveName = .... anywhere before you do src.Close.

Excel Checkbox Selected sheets to Another Worksheet

I need help in making some changes to the existing VBA.
Based on the "Sheet" name and Sheet No., the existing code was used to copy a selected checkbox sheet to a new workbook.
I'd like to make it flexible so that the code still works when I rename or create a new sheet.
Private Sub ExportSheet_Click()
Dim wbNew As Workbook
Dim ws As Worksheet
Dim cnt As Control
Dim fileName As String
Const chBx As String = "CheckBox"
Application.ScreenUpdating = False
fileName = ThisWorkbook.Path & "\" & Me.Cmbteam.Value & ".xlsx"
Set wbNew = Workbooks.Add
Application.DisplayAlerts = False
Do Until wbNew.Worksheets.Count = 1
wbNew.Worksheets(2).Delete
Loop
Application.DisplayAlerts = True
Set ws = wbNew.Worksheets(1)
ws.Name = "DELETE_ME_LATER"
For Each cnt In Me.Controls
If Left(cnt.Name, Len(chBx)) = chBx Then
If cnt.Value = True Then
ThisWorkbook.Worksheets("Sheet" & Mid(cnt.Name, Len(chBx) + 1)).Copy after:=wbNew.Worksheets(wbNew.Worksheets.Count)
End If
End If
Next cnt
If wbNew.Worksheets.Count = 1 Then
MsgBox "No boxes were checked." & vbNewLine & "No file created", vbExclamation + vbOKOnly, "No selection made"
wbNew.Close savechanges:=False
Else
Application.DisplayAlerts = False
'Delete dummy sheet
ws.Delete
wbNew.Close savechanges:=True, Filename:=fileName
Application.DisplayAlerts = True
MsgBox "File saved at:" & vbNewLine & fileName
Unload Me
End If
Application.ScreenUpdating = True
End Sub

Export Multiple Sheets to CSV

I am using this code which exports activesheet to CSV. However, I am looking to modify this so I can pass as arguments the names of multiple sheets to export.
Sometimes it could be 2 sheets, sometimes it could be 10 sheets and I want to somehow define the names of the sheets as parameters for the export.
Sub saveSheetToCSV()
Dim myCSVFileName As String
Dim tempWB As Workbook
Application.DisplayAlerts = False
On Error GoTo err
myCSVFileName = ThisWorkbook.Path & "\" & "CSV-Exported-File-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
ThisWorkbook.Sheets("YourSheetToCopy").Activate
ActiveSheet.Copy
Set tempWB = ActiveWorkbook
With tempWB
.SaveAs Filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False
.Close
End With
err:
Application.DisplayAlerts = True
End Sub
Export Worksheet to New Workbook
!!! denotes places to be checked carefully and possibly modified.
Option Explicit
Sub ExportWorksheetsTEST()
Dim wb As Workbook: Set wb = Workbooks.Open("C:\Test\Test.xlsx")
ExportWorksheets "Sheet1", "Sheet5", "Sheet8"
End Sub
Sub ExportWorksheets(ParamArray WorkSheetNames() As Variant)
Dim dFolderPath As String: dFolderPath = ThisWorkbook.Path & "\"
Const dFileExtension As String = ".csv"
Const dDateFormat As String = "dd-MMM-yyyy hh-mm"
Const dFileNameDelimiter As String = "-"
' This is the requirement.
' The recommendation is to put it as the first parameter of the procedure:
' Sub ExportWorksheets(ByVal wb As Workbook, ParamArray...)!!!
Dim wb As Workbook: Set wb = ActiveWorkbook
Dim dDateString As String: dDateString = VBA.Format(VBA.Now, dDateFormat)
Dim ws As Worksheet
Dim n As Long
Dim dFilePath As String
For n = LBound(WorkSheetNames) To UBound(WorkSheetNames)
On Error Resume Next ' prevent error if worksheet doesn't exist
Set ws = wb.Worksheets(WorkSheetNames(n))
On Error GoTo 0
If Not ws Is Nothing Then
' Build the file path!!!
dFilePath = dFolderPath & ws.Name & dFileNameDelimiter _
& dDateString & dFileExtension
ws.Copy ' copy to a new workbook
With Workbooks(Workbooks.Count)
Application.DisplayAlerts = False ' overwrite w/o confirmation
.SaveAs Filename:=dFilePath, FileFormat:=xlCSV
Application.DisplayAlerts = True
.Close SaveChanges:=False
End With
Set ws = Nothing
End If
Next n
MsgBox "Worksheets exported.", vbInformation
End Sub

VBA to copy workbook and keep relative cell references between sheets

I have workbook that has multiple sheets and need a macro button to save a copy of it and delete the sheet named "CSG". This was easy to do, but the problem was that all cell references pointed to the original workbook.
With help, the problem has been tried to solve through name manager and break all links-code. Now the problem is that it break all references within the new workbook and copies only the values from the original workbook.
For example, in the original workbook sheet1 cell A1 has value 10, sheet2 cell A1 has cell reference "='sheet1'!A1". When I make the new copy, both cells do have the value 10, but the reference is no longer there.
Is there a way to keep these references within the workbook without them referencing the original workbook? Below is the code currently being used.
Sub SaveTest()
Dim x As Integer
Dim FileName As String, FilePath As String
Dim NewWorkBook As Workbook, OldWorkBook As Workbook
Set OldWorkBook = ThisWorkbook
With Application
.ScreenUpdating = False: .DisplayAlerts = False
End With
On Error Resume Next
With OldWorkBook.Sheets("CSG")
FilePath = "C:\Users\Tom\Desktop\" & .Range("B1").Value & " " & .Range("B2").Value
FileName = .Range("B1").Value & " " & .Range("B2").Value & ".xlsx"
End With
MkDir FilePath
On Error GoTo -1
On Error GoTo myerror
FilePath = FilePath & "\"
For x = 2 To OldWorkBook.Worksheets.Count
With OldWorkBook.Worksheets(x)
If Not NewWorkBook Is Nothing Then
.Copy after:=NewWorkBook.Worksheets(NewWorkBook.Worksheets.Count)
Else
.Copy
Set NewWorkBook = ActiveWorkbook
End If
End With
Next x
DeleteBadNames NewWorkBook
BreakAllLinks NewWorkBook
UpdateNameManager NewWorkBook
NewWorkBook.SaveAs FilePath & FileName, 51
myerror:
If Not NewWorkBook Is Nothing Then NewWorkBook.Close False
With Application
.ScreenUpdating = True: .DisplayAlerts = True
End With
If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub
Create a Copy of a Workbook
Option Explicit
Sub SaveTest()
Dim OldWorkBook As Workbook: Set OldWorkBook = ThisWorkbook
Dim WorkSheetNames() As String
Dim FilePath As String
Dim FileName As String
With OldWorkBook.Worksheets("CSG")
ReDim WorkSheetNames(1 To .Parent.Worksheets.Count)
FilePath = "C:\Users\Tom\Desktop\" & .Range("B1").Value & " " _
& .Range("B2").Value
FileName = .Range("B1").Value & " " & .Range("B2").Value & ".xlsx"
End With
On Error Resume Next
MkDir FilePath
On Error GoTo 0
FilePath = FilePath & "\"
Dim ws As Worksheet
Dim n As Long
For Each ws In OldWorkBook.Worksheets
n = n + 1
WorkSheetNames(n) = ws.Name
Next ws
Application.ScreenUpdating = False
OldWorkBook.Worksheets(WorkSheetNames).Copy
With ActiveWorkbook ' new workbook
Application.DisplayAlerts = False
.Worksheets("CSG").Delete
.SaveAs FilePath & FileName, 51 ' xlOpenXMLWorkbook
Application.DisplayAlerts = True
'.Close SaveChanges:=False
End With
Application.ScreenUpdating = True
End Sub

Moving sheet from one WB to another New WB

I have made the following code for moving sheet from one WB to another New WB.
However I am experiencing errors.
Sub MoveSheets01()
Dim ws As Worksheet
Dim newWB As Workbook
Dim oldwb As Workbook
Application.ScreenUpdating = False
Set oldwb = ActiveWorkbook
Set newWB = Application.Workbooks.Add
oldwb.Activate
For Each ws In oldwb.Sheets
If ws.Name <> "Input" And ws.Name <> "Output" Then
Application.DisplayAlerts = False
ws.Copy after:=newWB.Sheets(newWB.Sheets.Count)
ws.Delete
Application.DisplayAlerts = True
End If
Next ws
oldwb.Save
newWB.Activate
Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Application.DisplayAlerts = True
newWB.SaveAs Filename:=oldwb.Path & "\AAA " & Format(Now(), "DD.MMM.YYYY hh.mm AMPM") & ".xlsx", CreateBackup:=False
End Sub
It generates a new WB.
But the moment I save either, the file crashes.
Try this. See comments, especially regarding the filename. Most likely you have too long filename. If error occurs - post a comment with error text.
Sub MoveSheets010()
Dim ws As Worksheet
Dim newWB As Workbook
Dim oldwb As Workbook
Dim link As Variant
' switch this off for the whole sub
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
Set oldwb = ThisWorkbook
For Each ws In oldwb.Sheets
If ws.Name <> "Input" And ws.Name <> "List" _
And ws.Name <> "Temp" And ws.Name <> "Index Data" _
And ws.Name <> "Ratio's" And ws.Name <> "Total Returns Index" _
And ws.Name <> "India VIX" And ws.Name <> "Output" Then
' check whether newWB is assigned
If Not newWB Is Nothing Then
' if assigned - just add sheet there
ws.Move before:=newWB.Sheets(1)
Else
' if not assign - create new workbook by moving the sheet
' this creates new workbook with only one sheet
' so there will be no "Sheet1", "Sheet2", etc
ws.Move
' assign newWB
Set newWB = ActiveWorkbook
End If
End If
Next
Set ws = Nothing
' save new wb first to avoid message about links/references
newWB.SaveAs Filename:=oldwb.Path & "\AAA " & Format(Now(), "DD.MMM.YYYY hh.mm AMPM") & ".xlsx", CreateBackup:=False
' remove references from source wb and save it
With oldwb
If Not IsEmpty(.LinkSources(xlExcelLinks)) Then
For Each link In .LinkSources(xlExcelLinks)
.BreakLink link, xlLinkTypeExcelLinks
Next link
End If
.Save
End With
' switch this on back
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub

Resources