Excel-VBA file operation 1

Source: Internet
Author: User

In our daily use of Excel, we not only use the data of the current Excel file, but also frequently need to access other data files. These data files may be Excel files, text files, or database files. Some people often ask how to operate these data files in the VBA code? This article systematically introduces how to use VBA to operate data files in Excel.

This document describes four common methods:

1. Use Excel objects to process files;

2. Use VBA file processing statements to process files;

3. Use the FileSystemObject object to process files;

4. Use API functions to process files.

Of course, you can also use ADO + SQL to operate database files. However, the Forum has already described such methods in detail, so this article will not repeat them.

1. Use Excel objects to process files

Using the built-in methods of Excel objects to operate files is the most convenient and simple.

We mainly use workbooks collections and workbook objects to operate files.

1. Open an Excel file

You can use workbooks. Open to open an Excel Workbook.

Workbooks. Open (filename, updatelinks, readonly, format, password, writerespassword, ignorereadonlyrecommended, origin, delimiter, editable, y, converter, addtomru, local, corruptload)

Filename is a required parameter, indicating the name of the workbook to be opened. If no path is specified, it indicates the current path. The other 14 are optional parameters, except Password parameters, which are rarely used. For specific meanings, see the help of VBA.

Example:

Workbooks. Open "F: test.xls"

Open the test.xls file on the fdisk.

2. Open a text file

You can use the open method to open text files, but we recommend that you use the opentext method. This method loads a text file and processes it as a workbook containing a single worksheet in a separate manner, and then puts the processed text file data in this worksheet. The complete syntax is as follows:

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.