The macro below is SUPPOSED to save only the "Calculation" sheet to the file path listed in cell N5 (Calculation Sheet) and then show a message box showing where it was saved. For some reason the file is saving to either the last opened folder or the "My Documents" folder (no matter what I put in cell "N5" on the "Calculation sheet). However, the message box shows the file path that I have typed in cell N5. I cobbled this code together and have limited experience at VBA so it's time to call in an expert.
See attached worksheet.
Sheets("Calculation").Select
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the sheet to a new workbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2013
FileExtStr = ".xlsx": FileFormatNum = 51
End If
End With
'Change all cells in the worksheet to values if you want
With Destwb.Sheets(1).UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
'Save the new workbook and close it
TempFilePath = Sheets("Calculation").Range("N5").Value
TempFileName = Range("N4").Value
With Destwb
.SaveAs TempFileName & FileExtStr, FileFormat:=FileFormatNum
.Close SaveChanges:=False
End With
MsgBox "You can find the new file in " & TempFilePath
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
I will actually need two different variations of this macro: 1). Macro as noted in my description above. 2). Macro to do basically the same thing (insert file name listed in "N4", in the file name box and go to file path listed in "N5") but stop at the SaveAs file dialog box so that the user can change the file name manually or the file path if they want to.
Thanks in advance for your help.
Take care.
Marvin
Bookmarks