×

VBA ActiveCell Property

What is the ActiveCell Property?

The active cell signifies the active selected cell in the current worksheet. The Active property acts as a reference point and is used to move the cell cursor from one location to another. It is also useful in changing the current cell or reference cell properties. The cell address of the Active cell will also be displayed in the name box (left corner of the excel sheet) For instance, and we have selected the B4 cell in sheet 1 means cell B4 is the active cell of this worksheet and we fetch range properties of cell B4 by using Active cell function. In the below example, you will notice in the name box B4 cell address has been displayed.

VBA ActiveCell Property

In VBA, the Active cell function is used in different circumstances where we need to alter the properties/values in the active cell or want to apply certain conditions to meet the requirements. This function is also used to assign values or to fetch the address details of an active cell. It returns the range property of the current of the active worksheet.  The Active cell function can also be accessible with the help of active cell application.property method.

Many users confuse the active cell property with selection. But these terms are different and are related to each other. The selection contains one or more cells, whereas active cell can have only one cell. It could be there inside the selection range.

Many users confuse the active cell property with selection. But these terms are different and are related to each other. The selection contains one or more cells whereas active cell can have only one cell. It could be there inside the selection range.

Advantages of ActiveCell Property

  1. ActiveCell acts as a starting point and is often used to offset the cursor anywhere as per the requirement.
  2. With the help of this property, we can easily change all the features of the active cell or the referenced cells
  3. The Activecell property helps us to fetch the row, column, and cell address.

Syntax

  1. Basic Activecell
ActiveCell ([RowIndex], [ColumnIndex])
  • Assign the value to Active cell
Activecell.Value= “”
  • Row/Column/Value - Select Active cell’s value or property in the current workbook. The following syntax is also used to display row or column address of the active cell
Application.Activecell
  • Font selection - The following syntax is used to change the text font of the active cell. This is done by using the font property.
Activecell.Font.(font_name) = True

ActiveCell Example

Code:

Sub ActiveCell_Example()
 'activating the current sheet with the help of the activate method
 Worksheets("Sheet1").Activate
 'passing value to our active cell
 ActiveCell.Value = "Hello World"
 End Sub 

Let’s us analyze step-step the above VBA code:

Step 1: Open the developer window by using the shortcut keywords Alt +F11.

Step 2: Create a module by right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA ActiveCell Property

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

VBA ActiveCell Property

Step 4: We will specify the worksheet which you would like to activate. This is optional if you want to can skip this step. In the below example, we have specified Sheet1.

VBA ActiveCell Property

Step 5: Next, with the help of the ActiveCell function, we will set a value. This will display the same value in the current cell address.

VBA ActiveCell Property

Output

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

Step 7: You will notice that in the Excel active cell the value “Hello World” is written.

VBA ActiveCell Property

Active Cell Address, Value, Row, and Column Number

Active cell function helps us to fetch the address, row, and column number. All the information can be fetched through the ActiveCell method i.e., ActiveCell.Row, ActiveCell.Column, ActiceCell.Value, etc.,

Code:

Sub ActiveCell_RowColumnNumber()
 'declaring the variable
 Dim currentCell As Variant
 'Activating the worksheet sheet1
 Worksheets("Sheet1").Activate
 'string the range of active cell in variable currentCell
 Set currentCell = Application.ActiveCell 
 'fetching the current row address of the active cell
 MsgBox ("Active cell row number = " & currentCell.Row)
 'fetching the current row address of the active cell
 MsgBox ("Active cell column number = " & currentCell.Column)
 End Sub 

Let’s us analyze step-step the above VBA code:

Step 1: Open the developer window by using the shortcut keywords Alt +F11.

Step 2: Create a module by right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA ActiveCell Property

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

VBA ActiveCell Property

Step 4: Declare the variable so as further you can store the return range. Also, we will activate the worksheet.

VBA ActiveCell Property

Step 5: Call the Application.ActiveCell function and store the returned value in the declared variable.

VBA ActiveCell Property

Step 6: With the help of the Row and column method, we will fetch the ActiveCell address.

Output

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

Step 7: The message box dialog box will appear displaying the row number. Click on OK.

VBA ActiveCell Property

Step 8: The message box dialog box will again appear displaying the column number.

VBA ActiveCell Property

ActiveCell with Fonts

You can use all the Fonts properties to alter the ActiveCell look and feel and can make it more representable.

Code:

Sub ActiveCell_Fonts()
 'changing the font of the active cell with ActiveCell.Font method
 'Using With statement to perform a series of font properties
 With ActiveCell.Font
  .Bold = True
  .Italic = True
  .ColorIndex = 21 
  .FontStyle = "Algerian"
  .Size = 14
 End With
 End Sub 

Let’s us analyze step-step the above VBA code:

Step 1: Open the developer window by using the shortcut keywords Alt +F11.

Step 2: Create a module by right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA ActiveCell Property

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

VBA ActiveCell Property

Step 4: Activating the worksheet (Sheet1) with the help of Worksheets.Activate method.

VBA ActiveCell Property

Step 5: Next, we will use all the fonts properties with the help of ActiveCell.Font method.  In the below example, we have used ‘With’ statement to perform a series of fonts properties.

VBA ActiveCell Property

Output

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

Step 7: You will notice that the active cell value has changed its fonts properties.

VBA ActiveCell Property

Related Topics

Excel VBA InStr Function

VBA InStr Function The InStr function in VBA searches for a substring inside the given string. It returns the position of a substring within a string, as an integer if the substring is found...

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 CVErr Function

VBA CVErr Function: The CVErr function in VBA returns an Error data type, involving with a user-specified error code. Syntax CVErr (Expression) Parameter Expression (required)- This parameter represents the required error code. Return This function returns an...

1 minute read.

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to...

4 minutes read.

Excel VBA Val Function

VBA Val Function: The Val function in VBA converts the given string into a numeric value. This function ignores spaces and continues to read the characters after space(s). It stops...

1 minute read.

Excel VBA UCase Function

The UCase function in VBA converts a String into upper case text. Syntax UCase (String) Parameter String (required) – This parameter represents the string that you want to convert to upper case. Return This function returns a string...

1 minute read.

VBA Updating Pivot Table

VBA- Updating Pivot Table The pivot table is an important feature to explore, summarize, and interpret the bulk amount of data. It helps data evaluating, reviewing, as well as making useful...

3 minutes read.

VBA Charts

What is a Chart? A chart is used to visually show numbers or data in a spreadsheet (or spread over multiple spreadsheets) so that the end-user can look at the chart...

3 minutes read.

Excel VBA CStr Function

VBA CStr Function: The CStr function in VBA converts an expression into a string data type. Syntax CStr (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to a...

1 minute read.

Excel VBA Replace Function

VBA Replace Function: The Replace function in VBA searches for a substring within the specified string and replaces its occurrences with a second substring. Syntax Replace (Expression, Find, Replace, [Start], [Count], [Compare]) Parameter Expression (required) – This parameter represents...

2 minutes read.

Steps to Create a Chart in VBA

Steps to Create a Chart Charts are created either by directly working with the chart variable object that defines the chart data or by ChartObject method. In order to get to...

3 minutes read.

ActiveX Controls

ActiveX Controls are one of the most used Excel Controls to automate applications with Excel VBA. It has the same controls, unlike Form Controls (Command Button, combo box, checkbox, etc.), but it...

3 minutes read.

VBA Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 minutes read.

VBA Pivot Table Grouping

VBA- Pivot Table Grouping For an instance, if in our pivot table, we have 11 different age groups from 20 to 30 -  but there might be a possibility that we...

2 minutes read.

Excel VBA Now Function

Excel VBA Now Function: The Now function in VBA returns the current date and time.  Syntax Now () Parameter NA Return This function returns the current date and time.  Example 1 Sub NowFunction_Example1() 'It will return...

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.

Excel VBA StrComp Function

VBA StrComp Function: The StrComp function in VBA compares two strings and returns an integer value displaying the result of the comparison. Syntax StrComp (String1, String2, [Compare]) Parameter String1 (required)- This parameter represents the first string to...

2 minutes read.

Excel VBA : With End-with Constructs

With End-with Constructs The With-End With construct enables the user to perform multiple operations on a single object. If you are going to perform several different actions on the same object and typing the same...

4 minutes read.

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

Excel VBA CByte Function

VBA CByte Function: The CByte function in VBA converts an expression into a Byte data type. Syntax CByte (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.