×

VBA ActiveSheet

What is VBA Activesheet Property?

The active sheet means the current worksheet which you are working on and viewing. The ActiveSheet object signifies the worksheet tab that is selected before running the VBA code. If the user is working with multiple sheets, then the currently viewed files are considered as the active sheet. VBA facilitates many properties and methods that can be called using the ActiveSheet object.

By Default, Excel considered the only topmost worksheet as the ActiveSheet, and all the VBA coding is applicable only for the topmost worksheet. The user can mark or align any Excel worksheet as the Active sheet by calling the Activesheet object with the sheet name. One can also use the VBA Activate method to activate any Excel sheet.

People often confuse the Active worksheet with the selected sheet and use them interchangeably. But both have different functionalities.  Let’s evaluate the difference below:

Selected Worksheet  Active Worksheet
Selected Worksheet can select one or more Excel Worksheets within an Excel Window.   Active Worksheet only selects the current Worksheet to view and work upon.  
Each Workbook can have multiple Selected Worksheets At a time, only one active sheet can be selected.

Importance of Using ActiveSheet Object

  1. This property is useful when a user wants to use another sheet (to run the VBA code and modify the objects apart from the topmost worksheet.
  2. Many times we automate an Excel task with the help of multiple worksheets. In this situation, we can use Activesheet property to set the Activesheet variable and practice it for future reference.

Syntax

expression.ActiveSheet

where the expression variable represents an application object.

Return

This property returns a sheet object representing the active sheet in the current Excel Workbook. It returns null if the worksheet has no active sheet.

Read Data from ActiveSheet

One of the basic tasks in VBA’s day to day life is to read data from Excel ActiveSheet. The worksheet’s range object can be used to read data from the ActiveSheet Object.

Although, if you are referring to the topmost sheet, then it’s not necessary to specify ActiveSheet function before Range object.  In the below code Range (“B5”) can also read the data from ActiveSheet.

Code:

Sub ActiveSheet_ReadProperty()
 'reading the value of B5 cell in the ActiveSheet
 'with the help of Range object
 MsgBox ActiveSheet.Range("B5")
 'if ActiveCell object is not specified
 MsgBox Range("B5") 'it will return the same value as above
 End Sub 

Let’s work with the set-by-step code of lines:

Step 1: Open the VBA developer tab either by using the shortcut keywords Alt +F11 or click on developer window -> visual basic editor.

Step 2: Visual Basic Editor will open. Next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA ActiveSheet

Step 3: In the Module window, introduce the sub-block, followed by your macro name.

VBA ActiveSheet

Step 4: We will call the ActiveSheet function and to read the data will specify the Range object (specifying the cell or range in it.

VBA ActiveSheet

Step 5: With the help of ‘Msgbox’ will display the output.

VBA ActiveSheet

Step 6: We will repeat the above 2 steps, but this time will not specify the ActiveSheet object.

VBA ActiveSheet

Output

Step 7: Execute the above code either by pressing the F5 shortcut key or by clicking on the Run button.

Step 8: You will notice that the output has been displayed in the message box.

VBA ActiveSheet

Step 9: Press OK. Again, the message box dialog will be displayed, and both the outputs will be the same.

VBA ActiveSheet

ActiveSheet Name

The VBA  ‘.Name ‘property of the ActiveSheet fetches the name of your sheet. With the help of the name, you can proceed with many applications.

Code:

Sub ActiveSheet_NameProperty()
 'fetching the name of the ActiveSheet with help of
 'ActiveSheet.Name property
 MsgBox "The name of ActiveSheet is" & ActiveSheet.Name
 End Sub 

Let’s work with the set-by-step code of lines:

Step 1: Open the VBA developer tab either by using the shortcut keywords Alt +F11 or click on developer window -> visual basic editor.

Step 2: Visual Basic Editor will open. Next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA ActiveSheet

Step 3: In the Module window, introduce the sub-block, followed by your macro name.

VBA ActiveSheet

Step 4: With the help of MsgBox, we will display the name by using VBA ActiveSheet.Name property.

VBA ActiveSheet

Output

Step 5: Execute the above code either by pressing the F5 shortcut key or by clicking on the Run button.

Step 6: You will notice that the name has been displayed in the MsgBox.

VBA ActiveSheet

Password Protection in ActiveSheet

You can secure your ActiveSheet from unintended users and safeguard your data by using the VBA protect method. It will set a password in your current sheet. If needed, you can even remove it by using the VBA UnProtect method.

Syntax

  1. Protect Data
ActiveSheet.Protect ([Password], [DrawingObjects], …)
  • UnProtectData
ActiveSheet.UnProtect “password”

Code:

Sub ActiveSheet_ProtectProperty()
 'protect method is used to secure the
 'ActiveSheet with password
 ActiveSheet.Protect "password", True, True
 End Sub 

Let’s work with the set-by-step code of lines:

Step 1: Open the VBA developer tab either by using the shortcut keywords Alt +F11 or click on developer window -> visual basic editor.

Step 2: Visual Basic Editor will open. Next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA ActiveSheet

Step 3: Protect your Excel sheet with the help of ActiveSheet.Protect method.

VBA ActiveSheet

Output

Step 4: Run the output by clicking the F5 function key.

Step 5: you will notice you can no more write anything in the sheet. An alert dialogue box will pop up stating “The cell or chart you’re trying to change in on a protected sheet. To make a change, unprotect the sheet. You might be requested to enter a password”

VBA ActiveSheet

To Unprotect the Sheet

To again access/ write or format the sheet you must unprotect the sheet. To unprotect the Excel sheet, use the ActiveSheet. Unprotect method.

Code:

Sub ActiveSheet_UnProtectProperty()
 'protect method is used to secure the
 'ActiveSheet with password
 ActiveSheet.Protect "password", True, True
 'unprotecting the sheet
 ActiveSheet.Unprotect "password"
 End Sub 

Now your sheet has been unprotected.

ActiveSheet Clear Method

The ActiveSheet.Clear method is used to clear off all the content of the cells in the active cells.

Code:

Sub ActiveSheet_ClearProperty()
 'to clear the content of all the cells
 'in the Activesheet with the help of Clear method
 ActiveSheet.Cells.Clear
 End Sub 
VBA ActiveSheet

Output

Before the Clear method

VBA ActiveSheet

After the Clear method: All the content would be erased.

VBA ActiveSheet

Activate another sheet as ActiveSheet

We can active any random sheet as an ActiveSheet. For this, the ‘.Activate’ method is used.

Code:

Sub ActiveSheet_ActivateSheet()
 'activating Sheet2
 Sheet2.Activate
 'inserting value in cell B2
 ActiveSheet.Cells(2, 2) = "Hello ActiveSheet"
 End Sub 
VBA ActiveSheet

Output

You will notice that in Sheet2, at cell address B2, “Hello ActiveSheet” value has been entered.

VBA ActiveSheet

Related Topics

Excel VBA: IF THEN Statement

VBA Excel: IF THEN StatementThis conditional statement enables you to check one condition and on the basis of that then run one or multiple statements if the condition holds. If...

1 minute read.

Excel VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type). Syntax CSng (Expression) Parameter Expression (required) – This parameter represents the expression that you want...

1 minute read.

Excel VBA: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

Excel VBA Filter Function

VBA Filter Function: The Filter function in VBA returns a subset for the given string array, based on specified criteria. Syntax Filter (SourceArray, Match, [Include], [Compare]) Parameter SourceArray (required) – This parameter the array of Strings that you...

2 minutes read.

VBA Not Equal Operator

What is VBA Not Equal Operator? VBA Not Equal binary operator (“<>”) is a logical function that is used to check if the specified values are not equal or not. This...

5 minutes read.

Excel VBA RTrim Function

VBA RTrim Function: The Rtrim function in VBA removes the leading spaces from the text in the specified string. Syntax RTrim (String) Parameter String (required) – This parameter represents the string from which you want...

1 minute read.

Excel VBA UBound Function

Excel VBA UBound Function: The UBound function in VBA returns the highest subscript for the specified dimension in the given array. Syntax UBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

Excel VBA Cos Function

VBA Cos Function: The Cos function in VBA returns the cosine value for a supplied angle. Syntax Cos (Number) Parameter Number (required) –This parameter represents the number that you want the absolute value of. Return This function...

1 minute read.

Excel VBA Error Handling

What is Errors and Types of Error? Errors are conditions that resist the flow of the program or enables a problem while running any programming. There are three types of errors in VBA...

5 minutes read.

Excel VBA DateSerial Function

The DateSerial function in VBA returns a Date from a supplied year, month, and day number. Syntax DateSerial (Year, Month, Day) Parameter Year (required) – This parameter represents an integer signifying the year. Month (required) – This parameter...

2 minutes read.

Excel VBA CCur Function

The CCur function in VBA is used to convert an expression into a Currency data type. It can take a maximum of 15 digits to the left of the decimal place and...

1 minute read.

Excel VBA Oct Function

VBA Oct Function: The Oct function in VBA converts the given number into octal notation and returns the result as a string. Syntax Oct (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.

Excel VBA Fix Function

VBA Fix Function: The Fix function in VBA truncates the given number to an integer and returns the rounded off integer number. This function both positive and negative numbers to zero. Syntax Fix...

1 minute read.

VBA ActiveSheet

What is VBA Activesheet Property? The active sheet means the current worksheet which you are working on and viewing. The ActiveSheet object signifies the worksheet tab that is selected before running the VBA...

5 minutes read.

Excel VBA LBound Function

Excel VBA LBound Function: The LBound function in VBA returns the lowest subscript for the specified dimension in the given array. Syntax LBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

How to save Macro Workbook?

Saving Macro Workbook Default Excel File Extension The default excel file extension is “.xlsx”. But the standard file extension “.xlsx” cannot contains macros. So, the workbook contains macro when save din.xlsx file, all VBA...

1 minute read.

VBA Cell

What is VBA cell? Cells is one of the elements (workbook, worksheet, range) in Excel VBA, which refers to cells of the Excel worksheet.  In VBA, the cell is also a property...

5 minutes read.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

Excel VBA Right Function

VBA Right Function: The Right function in VBA returns a substring from the end of the given string. Syntax Right (Str, Length) Parameter Str (required) – This parameter represents the string from which you want to...

1 minute read.

Excel VBA Conditional Statement

Conditional Statement in VBA Excel Conditional Statements in Excel VBA are one of the most powerful and useful features in programming, this will give you to perform comparisons to decide or...

2 minutes read.