Using VBA to save multiple worksheets in Excel, select the region as a CSV file.

Source: Internet
Author: User
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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.