Saving excel worksheet to CSV with file name from

2020-04-16 04:51发布

i have a macro that saves a copy of my workbook that works but it saves as a .xlsm and i need it to be saved as a comma delimited .csv file type can any one help me?

here is the macro i have now

Sub toCSV()

Dim newWB As Variant
Dim wb1 As Workbook
Set wb1 = ActiveWorkbook
With wb1
    .SaveCopyAs ("C:\Users\sales\desktop\") & Range("A2").Text & ".xlsm"
End With

End Sub

3条回答
贼婆χ
2楼-- · 2020-04-16 05:21

Code:

.SaveAs Filename:=("C:\Users\sales\desktop\") & Range("A2").Text & ".csv", _
FileFormat=:xlCSV
查看更多
聊天终结者
3楼-- · 2020-04-16 05:24

This code will take the sheet you want to be saved as a CSV, and copy it to a new workbook before saving

Dim CSVBook As Workbook
Set CSVBook = Workbooks.Add
ThisWorkbook.Sheets("TheCSVSheet").Copy Before:=CSVBook.Sheets(1)
CSVBook.SaveAs Filename:="C:\tmp\test.csv", FileFormat:=xlCSV
CSVBook.Close

This will allow you to save the file as a CSV, but still retain the original macro enabled spreadsheet which you can go back to, and do whatever other processing you need

查看更多
我想做一个坏孩纸
4楼-- · 2020-04-16 05:42

Try adding this to your code:

FileFormat:=xlCSVMSDOS

And changing the '.xlsm' to '.csv'

I also highly suggest you look here for future questions with regard to saving!

查看更多
登录 后发表回答