To allow the user to pick where to save the file, you can use the Application.GetSaveAsFilename method in VBA. This method opens a "Save As" dialog box, allowing the user to choose the location and file name.
try this:
Sub SaveSheetAs()
' Select the "Monthly Summary1" sheet
Sheets("Monthly Summary1").Select
' Delete rows 58 to 75
Rows("58:75").Select
Selection.Delete Shift:=xlUp
' Copy the "Monthly Summary1" sheet to a new workbook
Dim wb As Workbook
Sheets("Monthly Summary1").Copy
Set wb = ActiveWorkbook
' Get the file name from cell B2
Dim fileName As String
fileName = Sheets("Monthly Summary1").Range("B2").Value
' Open the "Save As" dialog box
Dim saveAsFileName As Variant
saveAsFileName = Application.GetSaveAsFilename(InitialFileName:=fileName, FileFilter:="Excel Files (*.xlsx), *.xlsx")
' Check if the user canceled the "Save As" dialog box
If saveAsFileName <> False Then
' Save the new workbook with the selected file name and location
wb.SaveAs Filename:=saveAsFileName, FileFormat:=xlOpenXMLWorkbook
' Optionally close the new workbook
' wb.Close False
Else
' User canceled the "Save As" dialog box
MsgBox "Save As operation canceled."
End If
End Sub
Some explaination that might be helpful
Explanation:
- Selecting the Sheet and Deleting Rows:
- The code selects the "Monthly Summary1" sheet and deletes rows 58 to 75.
- Copying the Sheet to a New Workbook:
- The Sheets("Monthly Summary1").Copy line creates a new workbook containing a copy of the "Monthly Summary1" sheet.
- The Set wb = ActiveWorkbook line sets a reference to this new workbook.
- Getting the File Name:
- The fileName variable is set to the value from cell B2 of the "Monthly Summary1" sheet.
- Opening the "Save As" Dialog Box:
- The Application.GetSaveAsFilename method opens the "Save As" dialog box, with the initial file name set to the value from cell B2 and the file filter set to Excel files.
- The saveAsFileName variable stores the file name and path selected by the user.
- Saving the Workbook:
- If the user selects a file name and location, the workbook is saved using the wb.SaveAs method.
- If the user cancels the "Save As" dialog box, a message box informs them that the operation was canceled.
This code provides a user-friendly way to save the file, allowing the user to choose the location and file name through the "Save As" dialog box.
Hope this helps
plettieri