VBA to perform Save As, delete old data, and display new file

General Ledger

Active Member
Joined
Dec 31, 2007
Messages
460
Dear All,


I am trying to create a macro that will:
  1. Save data that was exported to Excel in a .xlsx file. The exported data could be xls, xlsx, csv, txt or tsv file format.
  2. Make that new .xlsx file the active workbook
  3. Close the file of exported data without saving
The code below (which I built from pieces I found in this forum) gets me close to my goal. However:
  • It opens the File Save dialog and not the Save As dialog. I don't know if this is a problem.
  • After execution, I am still looking at the exported file and not the .xlsx file that was saved.
  • Is there a way in this macro to maximize the Save As dialog?
Code:
Dim strFolder As String
 
    Filename = "Invoice Headers " & Format(Now, "yyyy-mm-dd hh mm") & ".xlsx"
 
With Application.FileDialog(msoFileDialogSaveAs)
  .AllowMultiSelect = False
  .InitialFileName = "Z:\Excel\" & Filename
  .InitialView = msoFileDialogViewDetails
  If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
End With

I am at best a novice so please be detailed in any responses.

Thank you,

GL
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I don't understand why you are calling the SaveAs dialog. That's not listed in your three criteria.

This saves the current workbook as an .xslx file.

Code:
    Dim strFileName As String, strPath As String
    
    strPath = "Z:\Excel\"
    strFileName = "Invoice Headers " & Format(Now, "yyyy-mm-dd hh mm") & ".xlsx"
    ActiveWorkbook.SaveAs Filename:=strPath & strFileName, FileFormat:=xlOpenXMLWorkbook

Use VBA SaveAs in Excel 2007-2010
 
Upvote 0
AlphaFrog,

The reason I use the Save As dialog is to give the user the opportunity to navigate and select a folder if the default location I provided ("Z:\Excel\") is not correct.

In the meantime, I found I was missing one important line of code: .Execute

My only remaining wish is code that will maximize the Save As dialog window.

Code:
Dim strFolder As String
 
    Filename = "Invoice Headers " & Format(Now, "yyyy-mm-dd hh mm") & ".xlsx"
 
With Application.FileDialog(msoFileDialogSaveAs)
  .AllowMultiSelect = False
  .InitialFileName = "Z:\Excel\" & Filename
  .InitialView = msoFileDialogViewDetails
  If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
  .Execute
End With

Thanks,

GL
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,814
Members
452,945
Latest member
Bib195

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