Excel VBA Lesson 20: The Worksheet Object

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditEmail this to someone

<<Lesson 19>> [Contents] <<Lesson 21>>

20.1 The Worksheet Properties in Excel VBA

Similar to the Range Object, the Worksheet object has its own set of properties and methods. When we write Excel VBA code involving the Worksheet object, we use Worksheets. The reason is that we are dealing with a collection of worksheets most of the time, so using Worksheets enables us to manipulate multiple worksheets at the same time.

Some of the common properties of the worksheet are name, count, cells, columns, rows and columnWidth.


Example 20.1

Private Sub CommandButton1_Click()

MsgBox Worksheets(1).Name

End Sub

The above example will cause a pop-up dialog that displays the worksheet name as sheet 1, as shown below:

vba_Figure16.1

Figure 16.1

The count property returns the number of worksheets in an opened workbook.

Example 20.2

Private Sub CommandButton1_Click()
MsgBox Worksheets.Count
End Sub

The output is shown in Figure 20.2.

vba_Figure16.2

Figure 20.2



Example 20.3

The count property in this example will return the number of columns in the worksheet.

Private Sub CommandButton1_Click()

MsgBox Worksheets(1).Columns.Count

End Sub

The output is shown below:

vba_Figure16.3

 Figure 20.3

Example 20.4

The count property in this example will return the number of rows in the worksheet.

Private Sub CommandButton1_Click()

MsgBox Worksheets(1).Rows.Count

End Sub

vba_Figure16.4

 Figure 20.4

 

20.2 The Worksheet Methods

Some of the worksheet methods are add, delete, select, SaveAs, copy, paste and more.

 Example 20.5

In this example, when the user clicks the first command button, it will add a new sheet to the workbook. When the user clicks the second command button, it will delete the new worksheet that has been added earlier.

Private Sub CommandButton1_Click()

Worksheets. Add

End Sub

Private Sub CommandButton2_Click()

Worksheets(1).Delete

End Sub

Example 20.6

The select method associated with worksheet lets the user select a particular worksheet. In this example, worksheet 2 will be selected.

Private Sub CommandButton1_Click()

‘Worksheet 2 will be selected

Worksheets(2).Select

End Sub

The select method can also be used together with the Worksheet’s properties Cells, Columns and Rows as shown in the following examples.

Example 20.7

Private Sub CommandButton1_Click()

‘Cell A1 will be selected

Worksheets (1).Cells (1).Select

End Sub

Example 20.8

Private Sub CommandButton1_Click()

‘Column 1 will be selected

Worksheets (1).Columns (1).Select

End Sub

Example 20.9

Private Sub CommandButton1_Click()

‘Row 1 will be selected

Worksheets (1).Rows (1).Select

End Sub

 

Excel VBA also allows us to write code for copy and paste. Let’s look at the following Example:

 Example 20.10

Private Sub CommandButton1_Click()

‘To copy the content of a cell 1

Worksheets(1).Cells(1).Select

Selection.Copy

End Sub

 

Private Sub CommandButton2_Click()

‘To paste the content of cell 1 to cell 2

Worksheets(1).Cells(2).Select

ActiveSheet.Paste
End Sub



<<Lesson 19>> [Contents] <<Lesson 21>>

 

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditEmail this to someone