vba to close file after changing from .xlsm to .xlsx file extension

phillipclark

Board Regular
Joined
Sep 10, 2013
Messages
65
Greetings All,
I am using the below code as a macro with an Image. The user clicks the image and the .xlsm file is saved with a specific file name and converted to an .xlsx file extension. It is here that i need the help. The original file closes " this is ok" the .xlsx file is left open. I need this file to close also.
Any help will be greatly appreciated.

Thank You in advance.

Code:
Sub SaveIt()
Application.DisplayAlerts = False
Dim dt As String, wbNam As String


wbNam = "Cookie Length Chart_"
dt = Format(CStr(Now), "yyyy_dd_mm_hh_mm AMPM")
ActiveWorkbook.SaveAs Filename:=wbNam & dt, FileFormat:=51
Application.DisplayAlerts = True




End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Greetings All,
I am using the below code as a macro with an Image. The user clicks the image and the .xlsm file is saved with a specific file name and converted to an .xlsx file extension. It is here that i need the help. The original file closes " this is ok" the .xlsx file is left open. I need this file to close also.
Any help will be greatly appreciated.

Thank You in advance.

Code:
Sub SaveIt()
Application.DisplayAlerts = False
Dim dt As String, wbNam As String


wbNam = "Cookie Length Chart_"
dt = Format(CStr(Now), "yyyy_dd_mm_hh_mm AMPM")
ActiveWorkbook.SaveAs Filename:=wbNam & dt, FileFormat:=51
Application.DisplayAlerts = True




End Sub

Since the workbook has been saved just add:
Code:
ThisWorkbook.Close savechanges:=False
 
Upvote 0
Good Morning,
Unfortunately I have tried this method and it doesn't function to close the new workbook after the Fileformat has been changed
 
Upvote 0
I think the issue is that once you change it to an xlsx, all VBA code is removed (you cannot have VBA code in an xlsx file).
So any VBA code after that line will be lost, so it will never run.

Typically, what I do in this situations is have a separate workbook to store my VBA code, and have it run on OTHER workbooks, rather than having the VBA code in the workbook whose extension you are trying to change.
 
Upvote 0
Good Afternoon Joe4 ,
I wasn't really thinking about that but makes sense once it changes to .xslx then the code cant be used. I like the idea of a separate workbook for the code and will try it out for my other projects. For this particular one I will simply change the file format back to 52 save as a macro enabled file and then work on any issues to prevent unwanted actions. Thank You for your advise and guidance.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top