Excel active cell column

Return to VBA Code Examples

This tutorial will demonstrate how to get the Active Cell’s column or row.

Active Cell Column

This procedure will return the ActiveCell’s column in a MessageBox:

Public Sub ActiveColumn()
  MsgBox ActiveCell.Column
End Sub

Active Cell Row

This will return the ActiveCell’s row:

Public Sub ActiveRow()
  MsgBox ActiveCell.Row
End Sub

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

Learn More!

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Active Cell in Excel VBA

The active cell is the currently selected cell in a worksheet. The active cell in VBA can be used as a reference to move to another cell or change the properties of the same active cell or the cell reference provided by the active cell. We can access an active cell in VBA by using the application.property method with the keyword active cell.

Understanding the concept of range object and cell properties in VBACells are cells of the worksheet, and in VBA, when we refer to cells as a range property, we refer to the same cells. In VBA concepts, cells are also the same, no different from normal excel cells.read more is important to work efficiently with VBA codingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more. One more concept you need to look into in these concepts is “VBA Active Cell.”

In Excel, there are millions of cells, and you are unsure which one is an active cell. For example, look at the below image.

Active cell address

In the above pic, we have many cells. Finding which one is an active cell is very simple; whichever cell is selected. It is called an “active cell” in VBA.

Look at the name boxIn Excel, the name box is located on the left side of the window and is used to give a name to a table or a cell. The name is usually the row character followed by the column number, such as cell A1.read more if your active cell is not visible in your window. It will show you the active cell address. For example, in the above image, the active cell address is B3.

Even when many cells are selected as a range of cells, whatever the first cell is in, the selection becomes the active cell. For example, look at the below image.

Active cell address range

Table of contents
  • Active Cell in Excel VBA
    • #1 – Referencing in Excel VBA
    • #2 – Active Cell Address, Value, Row, and Column Number
    • #3 – Parameters of Active Cell in Excel VBA
    • Recommended Articles

#1 – Referencing in Excel VBA

In our earlier articles, we have seen how to reference the cellsCell reference in excel is referring the other cells to a cell to use its values or properties. For instance, if we have data in cell A2 and want to use that in cell A1, use =A2 in cell A1, and this will copy the A2 value in A1.read more in VBA. By active cell property, we can refer to the cell.

For example, if we want to select cell A1 and insert the value “Hello,” we can write it in two ways. Below is the way of selecting the cell and inserting the value using the VBA “RANGE” object

Code:

Sub ActiveCell_Example1()

    Range("A1").Select
    Range("A1").Value = "Hello"
 
End Sub

VBA Active Cell Example 1

It will first select the cell A1 “Range(“A1″). Select”

Then, it will insert the value “Hello” in cell A1 Range(“A1”).Value = “Hello”

Now, we will remove the line Range(“A1”). Value = “Hello” and use the active cell property to insert the value.

Code:

Sub ActiveCell_Example1()

    Range("A1").Select
    ActiveCell.Value = "Hello"

End Sub

VBA Active Cell Example 1-1

Similarly, first, it will select the cell A1 “Range(“A1”). Select.

But here, we have used ActiveCell.Value = “Hello” instead of Range(“A1”).Value = “Hello”

We have used the active cell property because the moment we select cell A1 it becomes an active cell. So, we can use the Excel VBA active cell property to insert the value.

#2 – Active Cell Address, Value, Row, and Column Number

Let’s show the active cell’s address in the message box to understand it better. Now, look at the below image.

VBA Active Cell Example 2

In the above image, the active cell is “B3,” and the value is 55. So, let us write code in VBA to get the active cell’s address.

Code:

Sub ActiveCell_Example2()

    MsgBox ActiveCell.Address

End Sub

VBA Active Cell Example 2-1

Run this code using the F5 key or manually. Then, it will show the active cell’s address in a message box.

VBA Active Cell Example 2-2

Output:

VBA Active Cell Example 2-3

Similarly, the below code will show the value of the active cell.

Code:

Sub ActiveCell_Example2()

    MsgBox ActiveCell.Value

End Sub

VBA Active Cell Example 2-4

Output:

VBA Active Cell Example 2-5

The below code will show the row number of the active cell.

Code:

Sub ActiveCell_Example2()

  MsgBox ActiveCell.Row

End Sub

VBA Active Cell Example 2-6

Output:

Example 2-7

The below code will show the column number of the active cell.

Code:

Sub ActiveCell_Example2()

MsgBox ActiveCell.Column

End Sub

Example 2-8

Output:

VBA Active Cell Example 2-9

#3 – Parameters of Active Cell in Excel VBA

The active cell property has parameters as well. After entering the property, the active cell opens parenthesis to see the parameters.

VBA Active Cell Formula

Using this parameter, we can refer to another cell as well.

For example, ActiveCell (1,1) means whichever cell is active. If you want to move down one row to the bottom, you can use ActiveCell (2,1). Here 2 does not mean moving down two rows but rather just one row down. Similarly, if you want to move one column to the right, then this is the code ActiveCell (2,2)

Look at the below image.

VBA Active Cell Example 3

In the above image, the active cell is A2. To insert value to the active cell, you write this code.

Code:

ActiveCell.Value = “Hiiii” or ActiveCell (1,1).Value = “Hiiii”

VBA Active Cell Example 3-4

Run this code manually or through the F5 key. It will insert the value “Hiiii” into the cell.

Example 3-7

If you want to insert the same value to the below cell, you can use this code.

Code:

ActiveCell (2,1).Value = “Hiiii”

Example 3-5

It will insert the value to the cell below the active cell.

Example 3-2

You can use this code if you want to insert the value to one column right then.

Code:

ActiveCell (1,2).Value = “Hiiii”

Example 3-6

It will insert “Hiiii” to the next column cell of the active cell.

Example 3-3

Like this, we can reference the cells in VBA using the active cell property.

We hope you have enjoyed it. Thanks for your time with us.

You can download the VBA Active Cell Excel Template here:- VBA Active Cell Template

Recommended Articles

This article has been a guide to VBA Active Cell. Here, we learned the concept of an active cell to find the address of a cell. Also, we learned the parameters of the active cell in Excel VBA along with practical examples and a downloadable template. Below you can find some useful Excel VBA articles: –

  • VBA Selection
  • Excel Edit Cell Shortcut
  • Excel VBA Range Cells
  • Get Cell Value with Excel VBA

Свойство ActiveCell объекта Application, применяемое в VBA для возвращения активной ячейки, расположенной на активном листе в окне приложения Excel.

Свойство ActiveCell объекта Application возвращает объект Range, представляющий активную ячейку на активном листе в активном или указанном окне приложения Excel. Если окно не отображает лист, применение свойства Application.ActiveCell приведет к ошибке.

Если свойство ActiveCell применяется к активному окну приложения Excel, то идентификатор объекта (Application или ActiveWindow) можно в коде VBA Excel не указывать. Следующие выражения, скопированные с сайта разработчиков, являются эквивалентными:

ActiveCell

Application.ActiveCell

ActiveWindow.ActiveCell

Application.ActiveWindow.ActiveCell

Но если нам необходимо возвратить активную ячейку, находящуюся в неактивном окне приложения Excel, тогда без указания идентификатора объекта на обойтись:

Sub Primer1()

    With Windows(«Книга2.xlsx»)

        .ActiveCell = 325

        MsgBox .ActiveCell.Address

        MsgBox .ActiveCell.Value

    End With

End Sub

Программно сделать ячейку активной в VBA Excel можно с помощью методов Activate и Select.

Различие методов Activate и Select

Выберем программно диапазон «B2:E6» методом Select и выведем адрес активной ячейки:

Sub Primer2()

    Range(«B2:E6»).Select

    ActiveCell = ActiveCell.Address

End Sub

Результат:

Как видим, активной стала первая ячейка выбранного диапазона, расположенная слева вверху. Если мы поменяем местами границы диапазона (Range("E6:B2").Select), все равно активной станет та же первая ячейка.

Теперь сделаем активной ячейку «D4», расположенную внутри выделенного диапазона, с помощью метода Activate:

Sub Primer3()

    Range(«E6:B2»).Select

    Range(«D4»).Activate

    ActiveCell = ActiveCell.Address

End Sub

Результат:

Как видим, выбранный диапазон не изменился, а активная ячейка переместилась из первой ячейки выделенного диапазона в ячейку «D4».

И, наконец, выберем ячейку «D4», расположенную внутри выделенного диапазона, с помощью метода Select:

Sub Primer4()

    Range(«E6:B2»).Select

    Range(«D4»).Select

    ActiveCell = ActiveCell.Address

End Sub

Результат:

Как видим, ранее выбранный диапазон был заменен новым, состоящим из одной ячейки «D4». Такой же результат будет и при активации ячейки, расположенной вне выбранного диапазона, методом Activate:

Sub Primer5()

    Range(«E6:B2»).Select

    Range(«A3»).Activate

    ActiveCell = ActiveCell.Address

End Sub

Аналогично ведут себя методы Activate и Select при работе с выделенной группой рабочих листов.

Свойство Application.ActiveCell используется для обращения к одной ячейке, являющейся активной, а для работы со всеми ячейками выделенного диапазона используется свойство Application.Selection.


Home / VBA / How to use ActiveCell in VBA in Excel

In VBA, the active cell is a property that represents the cell that is active at the moment. When you select a cell or navigate to a cell and that green box covers that cell you can use ACTIVECELL property to refer to that cell in a VBA code. There are properties and methods that come with it.

Use the Active Cell Property

  1. Type the keyword “ActiveCell”.
  2. Type a dot (.) to get the list properties and methods.
  3. Select the property or method that you want to use.
  4. Run the code to perform the activity to the active cell.

Important Points

  • When you use the active cell property VBA refers to the active cell of the active workbook’s active sheet’s, irrespective of how many workbooks are open at the moment.
  • ActiveCell is ultimately a cell that comes with all the properties and methods that a normal cell comes with.

Activate a Cell from the Selected Range

To activate a cell using a VBA code there are two ways that you can use one “Activate” method and “Select” method.

Sub vba_activecell()

'select and entire range
Range("A1:A10").Select

'select the cell A3 from the selected range
Range("A3").Activate

'clears everything from the active cell
ActiveCell.Clear

End Sub

The above code, first of all, selects the range A1:A10 and then activates the cell A3 out of that and in the end, clears everything from the active cell i.e., A3.

Return Value from the Active Cell

The following code returns the value from the active cell using a message box.

MsgBox ActiveCell.Value

Or if you want to get the value from the active cell and paste it into a separate cell.

Range("A1") = ActiveCell.Value

Set Active Cell to a Variable

You can also set the active cell to the variable, just like the following example.

Sub vba_activecell()

'declares the variable as range
Dim myCell As Range

'set active cell to the variable
Set myCell = ActiveCell

'enter value in the active cell
myCell.Value = Done

End Sub

Get Row and Column Number of the ActiveCell

With the active cell there comes a row and column property that you can use to get the row and column number of the active cell.

MsgBox ActiveCell.Row

MsgBox ActiveCell.Column

Get Active Cell’s Address

You can use the address property to get the address of the active cell.

MsgBox ActiveCell.Address

When you run the above code, it shows you a message box with the cell address of the active cell of the active workbook’s active sheet (as I mentioned earlier).

Move from the Active Cell using Offset

With offset property, you can move to a cell which is a several rows and columns away from the active cell.

ActiveCell.Offset(2, 2).Select

Select a Range from the Active Cell

And you can also select a range starting from the active cell.

Range(ActiveCell.Offset(1, 1), ActiveCell.Offset(5, 5)).Select

More Tutorials

    • Count Rows using VBA in Excel
    • Excel VBA Font (Color, Size, Type, and Bold)
    • Excel VBA Hide and Unhide a Column or a Row
    • Excel VBA Range – Working with Range and Cells in VBA
    • Apply Borders on a Cell using VBA in Excel
    • Find Last Row, Column, and Cell using VBA in Excel
    • Insert a Row using VBA in Excel
    • Merge Cells in Excel using a VBA Code
    • Select a Range/Cell using VBA in Excel
    • SELECT ALL the Cells in a Worksheet using a VBA Code
    • Special Cells Method in VBA in Excel
    • UsedRange Property in VBA in Excel
    • VBA AutoFit (Rows, Column, or the Entire Worksheet)
    • VBA ClearContents (from a Cell, Range, or Entire Worksheet)
    • VBA Copy Range to Another Sheet + Workbook
    • VBA Enter Value in a Cell (Set, Get and Change)
    • VBA Insert Column (Single and Multiple)
    • VBA Named Range | (Static + from Selection + Dynamic)
    • VBA Range Offset
    • VBA Sort Range | (Descending, Multiple Columns, Sort Orientation
    • VBA Wrap Text (Cell, Range, and Entire Worksheet)
    • VBA Check IF a Cell is Empty + Multiple Cells

    ⇠ Back to What is VBA in Excel

    Helpful Links – Developer Tab – Visual Basic Editor – Run a Macro – Personal Macro Workbook – Excel Macro Recorder – VBA Interview Questions – VBA Codes

    In this post, you’ll be learning how you can get the active cell column and row in Excel spreadsheet using Excel VBA.

    To get the active cell’s column or row from VBA, use this code:

    To run the VBA code, you will be need to perform the below steps first.

    • Under the Developer tab, click on Visual Basics
    • In the Insert menu on the ribbon, click on the module options.
    • Enter the code in it, save the code and run it.

    Get the Active Cell Column

    Code

    For the cell’s column:

    Sub ActiveCellColumn()
    MsgBox ActiveCell.Column
    End Sub
    How to Get Active Cell Column and Row in Excel VBA?

    Active Cell Row

    For the cell’s row:

    Sub ActiveCellRow()
    MsgBox ActiveCell.Row
    End Sub
    How to Get Active Cell Column and Row in Excel VBA?

    Понравилась статья? Поделить с друзьями:
  • Excel activate cell if
  • Excel access and sql
  • Excel academy sf education отзывы
  • Excel absolute all cells
  • Excel 8 in 1 для кошек мультивитамины