VBA macro to save excel worksheet as a CSV file

K1bb5

New Member
Joined
May 16, 2013
Messages
1
fpath = "\\location\"
fname = "folder_" & Format(dte, "ddmmyyyy")

Set srcWkb = ActiveWorkbook

Worksheets("Thatworksheet").Copy

Set destWkb = ActiveWorkbook

With destWkb.Sheets("Thatworksheet").UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False


sfullpath = fpath & fname & ".csv"

ActiveWorkbook.SaveAs Filename:=sfullpath, FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close savechanges:=False


Unfortunatley this saves the file in the desired location but with .csv.err at the end so not the required .csv only format.

e.g. fname_15032023_20230316_144855.csv.err

I have tried different file formats to see if that could be the issue xlcsvWindows xlCSVMSDOS but thet too get a mysterious .err extension added on. The file location isn't restricted as can create a new file there.
I have also tried in sfullpath = fpath & fname only so as not to duplicate the .csv assignment in case that was issue.
Using O365 and 2016.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
fpath = "\\location\"
fname = "folder_" & Format(dte, "ddmmyyyy")

Set srcWkb = ActiveWorkbook

Worksheets("Thatworksheet").Copy

Set destWkb = ActiveWorkbook

With destWkb.Sheets("Thatworksheet").UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False


sfullpath = fpath & fname & ".csv"

ActiveWorkbook.SaveAs Filename:=sfullpath, FileFormat:=xlCSV, CreateBackup:=False
ActiveWorkbook.Close savechanges:=False


Unfortunatley this saves the file in the desired location but with .csv.err at the end so not the required .csv only format.

e.g. fname_15032023_20230316_144855.csv.err

I have tried different file formats to see if that could be the issue xlcsvWindows xlCSVMSDOS but thet too get a mysterious .err extension added on. The file location isn't restricted as can create a new file there.
I have also tried in sfullpath = fpath & fname only so as not to duplicate the .csv assignment in case that was issue.
Using O365 and 2016.
Hello @K1bb5

Where is fpath and fname are coming from?

try this:
Sub SaveAsCSV()
ActiveWorkbook.SaveAs Filename:="W:\ChangeThisToMatchYourPath\Book1.csv", FileFormat:=xlCSVMSDOS, CreateBackup:=False
End Sub

Make sure you make any adjustmnets that you need for example the path "W:\ChangeThisToMatchYourPath\" the file name "Book1.csv" and the FileFormat "xlCSVMSDOS" to match your needs!

Monty
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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