It is often necessary to convert an Excel worksheet into a CSV file. The Save As function can only be used to operate on one worksheet, And the whole worksheet is always saved, for files with multiple worksheets, especially many files that do not need to be saved to the CSV file, this processing method will be very troublesome, therefore, VBA is used to save the selection area of multiple Excel worksheets as a CSV file. First, select the worksheet to be exported in Excel (the method is: Click to select the first worksheet, and then press Ctrl and then click another worksheet). Then, run the following VBA macro and macro Code As follows:
View sourceprint? 01. Option explicit
02. sub exportselectiontocsv ()
03.
Dim wks as Worksheet
04.
Dim newwks as Worksheet
05.
For each wks in activewindow. selectedsheets
06.
Wks. Copy 'to a new workbook
07.
Set newwks = activesheet
08.
With newwks
09.
Application. displayalerts = false
10.
. Parent. saveas filename: = "C:/temp/" &. Name ,_
11.
Fileformat: = xlcsv
12.
Application. displayalerts = true
13.
. Parent. Close savechanges: = false
14.
End
15.
Next wks
16. End sub
The preceding VBA function saves the Excel worksheet to the folder C:/temp (which must already exist) and uses the worksheet name as the file name, the file with the same name will be overwritten (if a file with the same name exists ).
CSV is the comma separate value. This file format is often used as differentProgramThe format of data interaction between them.
OriginalArticleIf you reprint the data, please note:
Reprinted from hydrological tool set [http://www.cnhup.com]