×

Determine What Is Currently Selected With VBA

By Chris Newman •  Updated: 07/13/18 •  4 min read
How to determine what the user has currently selected with VBA

Why Is This Important?

When you are writing VBA macros that rely on a user’s selection, you want to make sure they have selected the proper object within Excel. Running code that is meant for a Chart when the user currently has a cell range selected could spell disaster for your routine. In this article, I am going to attempt to share with you as many methods as I can think of to determine if a specific object is selected.

If you are looking for a more dynamic solution, you can check out the VBA function I wrote to output what is currently selected.

Is A Range Selected?

Since Excel is filled with TONS of cells, chances are you will run into lots of scenarios where you may want to automate cell formats. The following VBA snippet shows you how to ensure your user has selected a cell range and not something else like a Chart or Shape prior to you modifying the selection.

Sub StoreSelectedRange()
'PURPOSE: Store user-selected range to a variable

Dim rng As Range

'Ensure the user has selected a cell range
  If TypeName(Selection) <> "Range" Then
    MsgBox "Please select a cell range before continuing"
    Exit Sub
  Else
    Set rng = Selection
  End If

End Sub

Is A Chart Selected?

Sub StoreSelectedChart()
'PURPOSE: Store user-selected Chart to a variable

Dim cht As Chart

If ActiveChart Is Nothing Then
  MsgBox "Please select a Chart before continuing"
  Exit Sub
Else
  Set cht = ActiveChart
End If

End Sub

Is A Chart Series Selected?

How about if you would like to modify a specific chart series within the currently selected chart? I have needed to do this in my AutoChart Excel add-in and myBrand Excel add-in where I allow users to easily change the color of a specific Chart Series. The following code will show you how to ensure a series within a chart is selected.

Sub StoreSelectedChartSeries()
'PURPOSE: Store user-selected Chart Series to a variable

Dim srs As Series

If TypeName(Selection) = "Series" Then
  Set srs = Selection
Else
  MsgBox "Please select a Chart Series before continuing"
  Exit Sub
End If

End Sub

Is A Table Selected?

Sub StoreSelectedTable()
'PURPOSE: Store user-selected table to a variable

Dim tbl As ListObject

On Error Resume Next
  Set tbl = ActiveCell.ListObject
On Error GoTo 0

If tbl Is Nothing Then
  MsgBox "Please select a cell within a table before continuing"
  Exit Sub
End If

End Sub

Is A Shape Selected?

What if you want to determine or modify the user’s currently selected shape? The following code will determine if there is a shape object selected and then store that shape to a variable called “shp” to allow you to easily manipulate the shape going forward.

Sub StoreSelectedShape()
'PURPOSE: Store user-selected Shape to a variable

Dim shp As Shape

  On Error Resume Next
    Set shp = ActiveSheet.Shapes(Selection.Name)
  On Error GoTo 0
  
  If shp Is Nothing Then
    MsgBox "Please select a Shape before continuing"
    Exit Sub
  End If

End Sub

Any Scenarios I Have Missed?

I tried to cover the main user-selection scenarios you may want to capture in your VBA code. If there are other scenarios you can think of, please point them out in the comments section below. I will look to add to this article when applicable.

Keep Learning

Chris Newman

Chris Newman

Chris is a finance professional and Excel MVP recognized by Microsoft since 2016. With his expertise, he founded TheSpreadsheetGuru blog to help fellow Excel users, where he shares his vast creative solutions & expertise. In addition, he has developed over 7 widely-used Excel Add-ins that have been embraced by individuals and companies worldwide.

[FREE Training] 10 Amazing Excel Efficiency Tricks. Why Don't People Know These?!

X