Excel VBA made Easy: -Liew Voon Kiong by Mark Allen

Excel VBA made Easy: -Liew Voon Kiong by Mark Allen

Author:Mark Allen [Allen, Mark]
Language: eng
Format: epub
Publisher: nguyenminh
Published: 2016-12-05T08:00:00+00:00


Chapter 9 Excel VBA Objects Part 2 –The Workbook Object

In the previous chapter, we have learned about Excel VBA objects and their properties and methods. In this chapter, we shall learn specifically about the Workbook object as it is one of the most important Excel VBA objects. It is also at the top of the hierarchy of the Excel VBA objects. We will deal with properties and methods associated the Workbook object.

9.1 The Workbook Properties. When we write VBA code involving the Workbook object, we use Workbooks. The reason is that we are dealing with a collection of workbooks most of the time, so using Workbooks enables us to manipulate multiple workbooks at the same time.

When will deal with multiple workbooks, we can use indices to denote different workbooks that are open, using the syntax Workbooks (i), where i is an index. For example, Workbooks (1) denotes Workbook1, Workbooks (2) denotes Workbook2 and more.

A workbook has a number of properties. Some of the common properties are Name, Path and FullName Let’s look at the following example:

Example 9.1

Private Sub CommandButton1_Click() MsgBox Workbooks(1).Name The program will cause a message dialog box to pop up and displays the first workbook name, i.e. workbook_object1.xls as shown in Figure 9.1 below:

Figure 9.1: The name of the Excel workbook

If we have only one open workbook, we can also use the syntax ThisWorkbook in place of Workbook (1), as follows: Private Sub CommandButton1_Click ()

MsgBox ThisWorkbook.Name

End Sub

Example 9.2 Private Sub CommandButton1_Click () MsgBox ThisWorkbook.Path

End Sub

Or you can use the following code

Private Sub CommandButton1Click ()

MsgBox Workbooks ("workbook_object1.xls").Path

End Sub

The output is shown below:

Figure 9.2: The path of the opened workbook

Example 9.3

This example will display the path and name of the opened workbook. The code is: Private Sub CommandButton1_Click ()

MsgBox ThisWorkbook.FullName

End Sub

Or Private Sub CommandButton1Click() MsgBox Workbooks("workbook_object1.xls").Fullname

End Sub

The output is shown in Figure 9.3.

Figure 9.3

9.2 The Workbook Methods

There are a number of methods associated with the workbook object. These methods are Save, SaveAs, Open, Close and more.

Example 9.4 In this example, when the user clicks on the command button, it will open up a dialog box and ask the user to specify a path and type in the file name, and then click the save button, not unlike the standard windows SaveAs dialog, as shown in Figure 9.5.

Private Sub CommandButton1_Click()

fName = Application.GetSaveAsFilename ThisWorkbook.SaveAs Filename:=fName

End Sub

Figure 9.4: The SaveAs dialog

Another method associated with the workbook object is open. The syntax is

Workbooks.Open ("File Name")

Example 9.5

In this example, when the user click on the command button, it wil open the file workbook_object1.xls under the pathC:\Users\liewvk\Documents\ Private Sub CommandButton1_Click() Workbooks.Open ("C:\Users\liewvk\Documents\workbook_object1.xls")

End Sub

The close method is the command that closes a workbook. The syntax is

Workbooks (i).Close

Example 9.6

In this example, when the user clicks the command button, it will close Workbooks (1). Private Sub CommandButton1_Click() Workbooks (1).Close

End Sub



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Eco-friendly approach of bio-indigo synthesis and developing purification methods towards isolation of indigo from indirubin and bacterial fragments by Ramalingam Manivannan & Kaliyan Prabakaran & Young-A Son(215108)
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(183666)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(91970)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(91652)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(91335)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74468)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50912)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(40288)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(40227)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(40114)
Alkaline-earth metals promote propane dehydrogenation with carbon dioxide through geometric effects: Altering the reaction pathway by unknow(32754)
Induced iron vacancies boosting FeOOH loaded on sustainable Fenton-like collagen fiber membrane for efficient removal of emerging contaminants by unknow(32538)
Efficient electric-field-assisted photochemical conversion of methane to n-propanol exclusively over penetrated TiO2Ti hollow fibers by Guanghui Feng(32473)
Bi2SiO5 nanosheets as piezo-photocatalyst for efficient degradation of 2,4-Dichlorophenol by Hangyu Shi & Yifu Li & Lishan Zhang & Guoguan Liu & Qian Zhang & Xuan Ru & Shan Zhong(32409)
A novel NDIPTA organic heterojunction photocatalyst with built-in electric field for efficient hydrogen production by Jiahui Yang & Baojun Ma & Yongfa Zhu(32382)
Enhanced conversion of methane to liquid-phase oxygenates via hollow ferrite nanotube@horseradish peroxidase based photoenzymatic catalysis by Jun Duan & Shiying Fan & Xinyong Li & Shaomin Liu(32348)
Ordered macroporous superstructure of defective carbon adorned with tiny cobalt sulfide for selective electrocatalytic hydrogenation of cinnamaldehyde by Xiao-Shi Yuan & Sheng-Hua Zhou & San-Mei Wang & Wenbo Wei & Xiaofang Li & Xin-Tao Wu & Qi-Long Zhu(32269)
What's Done in Darkness by Kayla Perrin(27163)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26554)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26486)