Code example of some Excel operations in VBA

Source: Internet
Author: User

Set objexcelapp = Createobject ("Excel. application ")
2objexcelapp. displayalerts = false
3objexcelapp. application. Visible = false
4objexcelapp. workbooks. Add
5 set objexcelbook = objexcelapp. activeworkbook
6 set objexcelsheets = objexcelbook. worksheets
7 set objspreadsheet = objexcelbook. Sheets (1)
8
9dim objfield, icol, irow
10 icol = 1' get the column number
11 irow = 1' get the row number
12objspreadsheet. cells (irow, icol). value = "use ASP to import data from access to an Excel file -- Western e" 'insert data into cells
13objspreadsheet. Columns (icol). shrinktofit = true' sets whether to automatically adapt to the table unit size (the cell width remains unchanged)
14' set the font in the Excel table
15objspreadsheet. cells (irow, icol). Font. Bold = true 'cell font bold
16objspreadsheet. cells (irow, icol). Font. italic = false' cell font skew
17objspreadsheet. cells (irow, icol). Font. size = 20 'set the cell font size
18objspreadsheet. cells (irow, icol). paragraphformat. Alignment = 1' sets the cell alignment format: Center
19objspreadsheet. cells (irow, icol). Font. Name = "'"' set the cell font
20objspreadsheet. cells (irow, icol). Font. colorindex = 2' sets the color of the cell text. The color can be queried, and 2 is white.
21objspreadsheet. Range ("A1: F1"). merge' merge cells (unit area)
22objspreadsheet. Range ("A1: F1"). Interior. colorindex = 1' background color of the design unit Network
23 'objspreadsheet. Range ("A2: F2"). wraptext = true' sets character rollback (automatic line feed)
24 irow = irow + 1
25for each objfield in objrs. Fields
26 'objspreadsheet. Columns (icol). shrinktofit = true
27objspreadsheet. cells (irow, icol). value = objfield. Name
28' set the font in the Excel table
29objspreadsheet. cells (irow, icol). Font. Bold = true
30objspreadsheet. cells (irow, icol). Font. italic = false
31objspreadsheet. cells (irow, icol). Font. size = 20
32objspreadsheet. cells (irow, icol). halignment = 2' Center
33 icol = icol + 1

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.