简体   繁体   中英

Data to .CSV file in VBA

I am using Microsoft Visual Basic for Application. Here I want to store my data into a .csv file.

Dim FileName As String
Dim PathName As String
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")
FileName = "filename.csv"
PathName = Application.ActiveWorkbook.Path
ws.Copy
ActiveWorkbook.SaveAs FileName:=PathName & "\" & FileName, _
    FileFormat:=xlCSV, CreateBackup:=False

By using the code we can convert the data to .csv file.

Error:Method 'Sheets' of object '_Global' failed

This is the error because there is no worksheets are active. But when I use this macros in Excel it will work. But I'm using it in Microsoft Visual Basic for Applications in CATIA. So I don't wanna introduce Excel variable here. It cause some compatibility issue. So please help me to create a csv file in VBA? Thanks in Advance.

Look on this site: http://www.coe.org/p/fo/et/thread=27438

There is a macro to export the settings from CATIA and they say there is a built-in option to export some settings.

If you need to create a csv file for any other reason, you should be able to extact the line of code from the macro thats posted there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM