Excel VBA important reference (original VBA code)

Source: Internet
Author: User

'Original VBACode(Important reference)

'1. Open Excel
Dim exl as new excel. Application ()
Exl. Visible = true

'2. Add a workbook
Exl. workbooks. Add ()

3. Set the activity Worksheet
'Exl. worksheets ("sheets"). Active ()

4. Open the specified Excel File
'Exl. workbooks. Open ("C: \ book1.xls ")

'5. display the Excel window
Exl. Visible = true

'6. Change the title bar
Exl. Caption = "aaaaaaaaaaa"

'7. assign values to cells
Exl. cells (1, 4). value = 100.
Exl. Range ("A1: B5"). value = 50

'8. Set the specified column width
Exl. activesheet. Columns (1). columnwidth = 20

'9. Set the Row Height
Exl. activesheet. Rows (1). rowheight = 1/0.035

'10. Insert a page in row 18th
Exl. activesheet. Rows (18). pagebreak = 1

'11. Delete the paging character before the 4th column
Exl. activesheet. Columns (4). pagebreak = 0

'12. Specify the border line width
Exl. activesheet. Range ("B3: D3"). Borders (2). Weight = 3

'13. border line type
Dim sheet as Excel. Worksheet
Sheet = exl. activesheet
Sheet. Range ("B10: f13"). Borders (Excel. xlbordersindex. xledgebottom). linestyle = 1

'14. Set footer
Sheet. pagesetup. centerfooter = "Page & P"

'15. Header
Sheet. pagesetup. centerheader = "aaaaaa"

'16. Top, bottom, left, right margin
Sheet. pagesetup. headermargin = 2/0.035
Sheet. pagesetup. footermargin = 2/0.035
Sheet. pagesetup. leftmargin = 2/0.035
Sheet. pagesetup. rightmargin = 2/0.035
'The header is at the top and footer is at the bottom.
Sheet. pagesetup. headermargin = 2/0.0354
Sheet. pagesetup. footermargin = 2/0.035

'22. The page is horizontally centered
Sheet. pagesetup. centerhorizontally = true
'23. Center the page vertically
Sheet. pagesetup. centervertically = true

'24. Page paper
Sheet. pagesetup. papersize = excel. xlpapersize. xlpapera4

'25. Print the grid (the border is solid, and the middle is dotted)
Sheet. pagesetup. printgridlines = true

'26. Copy the entire Worksheet
Sheet. Range ("A1: F5"). Copy ()
Sheet. Range ("A10"). pastespecial () 'Paste

'29. Insert columns/rows
Sheet. Columns (1). insert ()
Sheet. Rows (1). insert ()

'31. Merge Cells
Sheet. Range ("C4: D4"). Merge ()

'33. Adjust the font
'Sheet. cells ("A1: F10"). Font. Name = 5

'38. Print and preview
Exl. activeworkbook. printpreview ()

'39. Print
Exl. activeworkbook. printout ()

'40. Save the file
Exl. activeworkbook. saveas ("E: \ aa.xls ")

'41. Discard the disk
Exl. activeworkbook. Saved = true

'43. Exit
Exl. Quit ()

 

Sheet. pagesetup. leftheader: = '& B header left & B'; // bold
Sheet. pagesetup. centerheader: = '& D'; // display date
Sheet. pagesetup. rightheader: = 'page & P '; // page number
Sheet. pagesetup. centerfooter: = page & P; // footer

 

The header color Excel does not seem to be provided, and the font size is inverted:

EOLE = Createobject ("Excel. application ")
EOLE. workbooks. Add
EOLE. Visible =. T.
EOLE. cells (4, 4). value = 'test'
EOLE. activesheet. pagesetup. centerheader = "& 50 Report 1" & set the header
EOLE. activesheet. pagesetup. centerfooter = "& 28 page & P" & set footer

* & The following 50 and 28 can be customized to indicate the font size.


If you use cells instead of the header, you can set the font color as follows:

EOLE = Createobject ("Excel. application ")
EOLE. workbooks. Add
EOLE. Visible =. T.
EOLE. cells (1, 4). value = 'test'
EOLE. cells (1, 4). Font. size = 22
EOLE. cells (1, 4). Font. colorindex = 3

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.