prompt to save as not working in macro

thelad

Board Regular
Joined
Jan 28, 2011
Messages
245
Hi,

I have macro that moves sheets from one workbook to another and deltes certain contents of the moved sheet. I then rename the tabs on the two sheets. It is working fine.

However i would like at the end for a save as prompt to appear, so i can save it to a location. I have code for this and save as does appear but it doesnt seem to be saving the file.

Any ideas? It would appear it opens the save as before the file even opens itself. and when i do save as say workings, it will still be called book1.

Here is my code, any help is appreciated.

Code:
Sub CommandButton1_Click()
Dim OutlookApp As Object
Dim MItem As Object
Dim Wb As Workbook
Dim NewWb As Workbook
'Dim I As Long
Dim lastCol As Long
Dim delCol As Long
Dim myCols
Dim sName As String
Dim fil As Variant
 
Application.ScreenUpdating = False
 
 'Create Excel sheet link
Set Wb = ActiveWorkbook
 
Sheets(Array(Sheet2.Name, Sheet3.Name)).Copy
Set NewWb = ActiveWorkbook

myCols = Array("ID Number", "Par", "Identifier", "Date")
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
 
    For I = 0 To UBound(myCols)
        For delCol = lastCol To 1 Step -1
            If Cells(1, delCol) = myCols(I) Then
                Cells(1, delCol).EntireColumn.Delete
            End If
       Next
    Next I
sName = Application.InputBox("Enter the new sheet name:", Title:="New Sheet Title", Type:=2)
ActiveSheet.Name = sName
Worksheets("Sheet3").Name = "Ole Upload"
fil = Application.GetSaveAsFilename(FileFilter:="microsoft excel files (*.xls), *.xls")
If fil <> False Then
MsgBox "File Saved as " & fil
End If
        
Application.ScreenUpdating = True
        
        
End Sub
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You will need to add fil.Save to it

Code:
If fil <> False Then
 fil.Save
 MsgBox "File Saved as " & fil   'and/or depending on what you tying to do
 End If

Your code, only pulls up the dialouge box, which only sets the name and directory of the file, but doesnt actually save it.

fil would equal C:\excel.xls
fil.save would actually save it
 
Upvote 0
Hello thelad,

The Application.GetSaveAsFilename method only returns the selected file name and path that you chose. Despite the name, it does not save the file. Here is the amended the routine...
Rich (BB code):
fil = Application.GetSaveAsFilename(FileFilter:="microsoft excel files (*.xls), *.xls")
If fil <> False Then
   ThisWorkbook.Saves fil
   MsgBox "File Saved as " & fil
End If
Sincerely,
Leith Ross
 
Upvote 0
fil would equal C:\excel.xls
fil.save would actually save it

Not quite. As Leith points out, use it as the string (although I believe he is missing an "a" in "SaveAs"). So using a string as an object would return an error. But passing the string as the file name to save does work.
 
Upvote 0
Not quite. As Leith points out, use it as the string (although I believe he is missing an "a" in "SaveAs"). So using a string as an object would return an error. But passing the string as the file name to save does work.


Ah you're right, I just tested it, should be:

Code:
ThisWorkBook.SaveAs Filename:=fil
 
Upvote 0
Hello Zack,

Thanks for catching that. Seems I can't type today.

Sincerely,
Leith Ross
 
Upvote 0
Hi,

Thanks for your comments. It worked a treat. just had to change the active workbook to the new workbook.

thanks again
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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