Sub SaveSheetsToFiles()
Application.DisplayAlerts = True
Application.ScreenUpdating = False
Dim SummarySheet As Worksheet
Dim lastRow As Long
Dim i As Long
Dim SummaryMsg As String
'Set the summary sheet
Set SummarySheet = ThisWorkbook.Sheets("Summary")
'Find the last row with data in column A of the summary sheet
lastRow = SummarySheet.Cells(SummarySheet.Rows.Count, "C").End(xlUp).Row
'Loop through each row in the summary sheet
For i = 2 To lastRow 'Assuming row 1 is header
Dim sheetName As String
Dim fileName As String
Dim folderPath As String
'Get the sheet name and folder path from the summary sheet
sheetName = SummarySheet.Cells(i, "C").Value
folderPath = SummarySheet.Cells(i, "E").Value
fileName = SummarySheet.Cells(i, "D").Value
'Check if the sheet exists in the workbook
If SheetExists(sheetName) Then
'Copy the sheet to a new workbook
ThisWorkbook.Sheets(sheetName).Copy
'Copy and paste values only
With ActiveSheet.UsedRange
.Value = .Value
End With
'Save the new workbook with the specified name and path
ActiveWorkbook.saveas folderPath & "\" & fileName & ".xlsx"
' Add sheet name to summary message
SummaryMsg = SummaryMsg & sheetName & ">" & ActiveWorkbook.Name & " saved to " & folderPath & vbCrLf
'Close the new workbook without saving changes to it
ActiveWorkbook.Close False
End If
Next i
' Display summary message in a message box
MsgBox "Sheets saved successfully:" & vbCrLf & SummaryMsg, vbInformation
End Sub
Function SheetExists(sheetName As String) As Boolean
On Error Resume Next
SheetExists = Not Sheets(sheetName) Is Nothing
On Error GoTo 0
End Function