VBA – Work with Sheet/Worksheet

Spread the love

Sheets/Worksheet – it’s a very important to manage the code and determine to run on particular sheet. Method to use particular sheet and select it dynamically by code, by providing the sheet name, sequence number or property name of sheet can be used during the programming, but a good practice is to give a logical name of the worksheet, we tried to cover the maximum types of defining for a sheet in VBA

Sheets – target to a sheet by multiple methods.

 Selection of sheets in by sequence                            = Sheets(1).select
 Select a Sheet by name                                              = Sheets(“Sheet1”).select
 Use VBA sheet property (click on properties)             = Sheet1.select

Note : Add 3 worksheets in workbook, if it is 1

 

Execute the code one by one can differentiate that how a worksheet is being called by VBA script and getting changed.

‘Copy the below code and paste in module try to run manually with F8 Keyword

Sub SheetSelection()

‘=========it calls a sheet by its name=============
‘select a sheet / activate a sheet
Sheets(“sheet1”).Select
Sheets(“sheet2”).Select

‘============== it calls to active sheet , no reference is available ==========
ActiveSheet.Next.Select
ActiveSheet.Previous.Select
ActiveSheet.Next.Select

‘======= it is original property of sheet which VBA stores in memory ===========
Sheet3.Select

‘======== it follows the sequence of worksheets in an order ============

Sheets(1).Select ‘ sheet2 is 1 in the sequence
MsgBox “Your Active Sheet is ” & ActiveSheet.Name

End Sub