saveas workbook in vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
When I run the code below, I get this message

url
BvwJq.png


But that defeat the purpose. I mean, if I entered file name as Hello.xlsm, sill I get that message.
How can I improve that code?

By the way, if I save the file as "*.xlsm" before running that code and then when I run the code below, I wont get that massage. But if the file was a new (for example its name is Book1) then running the code below will get me that message. Thanks a lot.

Code:
Sub mysaveas()
    Dim xfile As String
    xfile = InputBox("enter file name with extension")
    Workbooks(1).SaveAs Filename:=xfile
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I think you need to specify the file format in the saveas routine.

Code:
Workbooks(1).SaveAs Filename:=xfile, FileFormat:=xlOpenXMLWorkbookMacroEnabled
 
Upvote 0
Do you always want to save as an xlsm file?
 
Upvote 0
Not really. If I can make it flexible that is even better. Thank you all for the help.
 
Upvote 0
Ok, how about
Code:
Sub mysaveas()
    Dim xfile As String
    Dim Frmt As Long
    xfile = InputBox("enter file name with extension")
    Select Case Mid(xfile, InStrRev(xfile, ".") + 1)
      Case "xls": Frmt = 56
      Case "xlsx": Frmt = 51
      Case "xlsm": Frmt = 52
      Case "xlsb": Frmt = 50
   End Select
   If Frmt = 0 Then Exit Sub
    Workbooks(1).SaveAs xfile, Frmt
End Sub
 
Upvote 0
Worked fine. Thank you a lot for the help that was great.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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