Saving a workbook as a .CSV in VBA

dwg83

Board Regular
Joined
Nov 8, 2006
Messages
174
I need to save a workbook as a .csv file while naming it with a ".mt1" ending. Currently the workbook saves, but not as comma seperated values. Additionally, when I get that to work, I'd like to not show the warning that says "TEST.mt1 may contain features that are not compatable...." I'd like to just automatically choose "yes". Thoughts???
This is what I have

Code:
fName = Application.GetSaveAsFilename(InitialFileName:=".mt1", FileFilter:="CSV (Comma delimited) (*.csv), *.csv", Title:="Save As")
ActiveWorkbook.SaveAs Filename:=fName

Thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
place
Code:
Application.DisplayAlerts = false

...code...

Application.DisplayAlerts = true

in your code to disable that comment and then instantly re enable it after your code is finished.

That is as far as I can help you I am actually going to look into your first line of code for I do not understand it.

edit: If you could site the source of where you got that first line of code that would be wonderful because I can not find anything in the excel help at all.
 
Last edited:
Upvote 0
Maybe like this?

Code:
Sub foo()

[COLOR="SeaGreen"]'//Get filename[/COLOR]
fname = Application.GetSaveAsFilename(InitialFileName:=".mt1", FileFilter:="CSV (Comma delimited) (*.csv), *.csv", Title:="Save As")
[COLOR="seagreen"]'//if default filter of .csv is supplied, change it to .mt1[/COLOR]
fname = Replace(fname, ".csv", ".mt1", 1, -1, vbTextCompare)
[COLOR="seagreen"]'//Save as .csv file[/COLOR]
ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlCSV

End Sub
 
Upvote 0
Alexander, when I run your code exactly, I am getting this error at the "ActiveWorkbook.SaveAs" line

Run-time error '1004':
Method 'SaveAs' of object '_Workbook' failed

Ideas?
 
Upvote 0
Hmmm...Works here. Maybe something about your filename? What are we trying to save the file as now?

Sub foo()

'//Get filename
fname = Application.GetSaveAsFilename(InitialFileName:=".mt1", FileFilter:="CSV (Comma delimited) (*.csv), *.csv", Title:="Save As")
'//if default filter of .csv is supplied, change it to .mt1
fname = Replace(fname, ".csv", ".mt1", 1, -1, vbTextCompare)
'//DEBUG - ADD THESE LINES
msgbox fname

'//Save as .csv file
ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlCSV

End Sub
 
Upvote 0
The message box gives the value of fname as C:\Documents and Settings\david\Desktop\TEST.mt1
 
Upvote 0
Very odd. You can save the file in the same place with the same name? I mean, on your own, rather than in code?
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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