We have seen different Objects in the previous tutorial, we will see the different examples on Workbook, Sheets, Range and Cell objects in this session.
Workbook Object in Excel VBA
The following are some example codes on Workbook Object, will help you dealing with various methods and properties of Excel workbook.
How to activate a Workbook
Yo can use Activate method to activate a Workbook as shown below:
Workbooks(“Workbook1.xlsx”).Activate
How to get the path of the Workbook
Yo can use FullName method of a Workbook as shown below:
Workbooks(“Workbook1.xlsx”).Path
How to get the full path (including name) of the Workbook
Yo can use FullName method of a Workbook as shown below:
Workbooks(“Workbook1.xlsx”).FullName
How to change the name of the Workbook
Workbooks(“Workbook1″).Name=”New Name”
Worksheet Object of Workbook in Excel VBA
The following are some example codes on Worksheet Object, will help you to deal with activating and selecting Excel worksheet.
How to Activate a Worksheet
Sheets(“Sheet2”).Activate
How to Select a Worksheet
Sheets(“Sheet2”).Select
How to rename a Worksheet
Sheets(“Sheet2″).Name=”NewSheet”
How to Hide a Worksheet
Sheets(“Sheet2”)..Visible = False
How to UnHide a Worksheet
Sheets(“Sheet2”).Visible = True
Range Object of Worksheet in Excel VBA
The following are some example codes on Range Object, will help you to deal with range colors, selection, etc.
How to Select a Range
Range(“A3”).Select
How to Enter Some Data in a Range
Range(“A3”).value=3000
How to change background color of Range
Range(“A1:E20”).Interior.ColorIndex=5
How to Clear a Range
Range(“A1:E20”).Clear
How to Copy a Range from one sheet to another sheet
Sheets(“Sheet1”).Range(“A1:E20”).Copy Destination:=Sheets(“Sheet2”).Range(“D1”)
Cells Object of Worksheet in Excel VBA
The following are some example codes on Cells Object.
How to Select a Range(“A3”)
Cells(3,1).Select
How to Enter Some Data in a Range(“A3”)
Cells(3,1).value=3000
How to change Font color of a Cell
Cells(3,1).Font.ColorIndex=5
How to Clear all Cells of a worksheet
Sheets(“Sheet1”).Cells.Clear
How to Clear only content not formats of all Cells of a worksheet
Sheets(“Sheet1”).Cells.ClearContents
Excelente work