Summary of common methods for manipulating Excel in VBA _vb

Source: Internet
Author: User

To use VBA to close the workbook, use the Workbook.close method, and to exit Excel, use the Application.Quit method.
Here are some code examples: Close the active workbook, and if the workbook has changes, prompts to save:

Copy Code code as follows:

Sub Closeworkbook ()
Activeworkbook.close
End Sub

If you want to avoid prompting, add the "SaveChanges" parameter, such as saving and closing the workbook directly:

Copy Code code as follows:

Sub Clostandsaveworkbook ()
Activeworkbook.close Savechanges:=true
End Sub

Change "True" in the preceding code to "False" to close the workbook directly without saving it.
Closes all open workbooks and prompts you to save:

Copy Code code as follows:

Sub Closeallworkbooks ()
On Error Resume Next
Workbooks.close
End Sub

Quit Excel and prompt you to save the workbook with changes:

Copy Code code as follows:

Sub Quitexcel ()
Application.Quit
End Sub

Save the active workbook and exit Excel:
Copy Code code as follows:

Sub Saveactiveworkandquit ()
Activeworkbook.save
Application.Quit
End Sub

The following code does not prompt you to save the workbook when you exit Excel.
Save all open workbooks and exit Excel:

Copy Code code as follows:

Sub Saveallandquit ()
For each wbk in workbooks
Wbk. Save
Next
Application.Quit
End Sub

Quit Excel and all changes are not saved:

Copy Code code as follows:

Sub Qutiandnoalerts ()
Application.DisplayAlerts = False
Application.Quit
End Sub

Some common operations for VBA are listed below:

Open a new workbook

Copy Code code as follows:

Workbooks.Add

Get the name of the first workbook

Copy Code code as follows:

Workbooks (1). Name

Get the number of open workbooks
Copy Code code as follows:

Workbooks.count

Activate a second open workbook

Copy Code code as follows:

Workbooks (2). Activate

Activate Workbook Chap02.xls

Copy Code code as follows:

Workbooks ("Chap02.xls"). Activate

The workbook that is currently active is Newchap.xls

Copy Code code as follows:

ActiveWorkbook.SaveAs filename:= "Newchap.xls"

Close the first workbook
Copy Code code as follows:

Workbooks (1). Close

Closes the currently active workbook without saving changes

Copy Code code as follows:

Activeworkbook.close Savechanges:=false

Close all open workbooks

Copy Code code as follows:

Workbooks.close

If you run the last example, all of your workbooks are now closed.
Before you use it on a worksheet, make sure you open a new workbook first.
When you're in addition to a single worksheet, you have to know how to add a new worksheet to the workbook, know how to select one or a group of worksheets, and know how to name, copy, move, and delete worksheets.
In VB, each task needs a special method or attribute.

Add a new worksheet

Copy Code code as follows:

Worksheets.add

Get the name of the first worksheet

Copy Code code as follows:

Worksheets (1). Name

Select a worksheet named "Sheet3"

Copy Code code as follows:

Worksheets (3). Select

Select First, third and fourth worksheets
Copy Code code as follows:
Worksheets (Array (1,3,4)). Select

Activate a worksheet named "Sheet1"

Copy Code code as follows:

Worksheets ("Sheet1"). Activate

Move Sheet "Sheet2" to Sheet "Sheet1"

Copy Code code as follows:

Worksheets ("Sheet2"). Move before:=worksheets ("Sheet1")

Rename sheet "Sheet2" to "expenses"

Copy Code code as follows:

Worksheets ("Sheet2"). Name = "Expenses"

Get the number of worksheets in the current workbook

Copy Code code as follows:

Worksheets.count

Delete Worksheet "Expenses" in the current workbook

Copy Code code as follows:

Worksheets ("expenses"). Delete

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.