VBA Active Cell | How to get Active cell in Excel

VBA Active Cell

Introduction/Problem Statement

Many times, we have required to find the row or column number of the active cells. In this blog, we will talk about how to find the VBA Active Cell.

Solution

1. Find Row number of the active cells.

This code will show the active row number in a message box.

Find Row number of active cell

Sub KDataScience_RowNumberOfActivecell()
    
    MsgBox ActiveCell.Row

End Sub

2. (a) Find the column number of the active cells.

This code will show an active column number in a message box

Find column number of active cell

Sub KDataScience_ColumnNumberOfActivecell()
    
    MsgBox ActiveCell.Column

End Sub

2. (b) Find the column Name of the active cell.

This code will return the column name of the active cell.

Find column Name of active cell

Sub KDataScience_ColumnNameOfActivecell()
    
    Dim addr As String
    Dim ColName As String
    'To Find the address of the active cell
    addr = ActiveCell.address
    'To split the address by $ and take first element of addr
    ColName = VBA.Split(addr, "$")(1)
    
End Sub

Conclusion

If you want to get (VBA active cell) column number or row number of the active cells of a worksheet then you can use any code shown above (according to one’s need).

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *