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.
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 you user has selected a cell range an not something else like a Chart or Shape prior to you modifying the selection.
'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"
Set rng = Selection
Is A Chart Selected?
'PURPOSE: Store user-selected Chart to a variable
Dim cht As Chart
If ActiveChart Is Nothing Then
MsgBox "Please select a Chart before continuing"
Set cht = ActiveChart
Is A Chart Series Selected?
How about if you would like 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.
'PURPOSE: Store user-selected Chart Series to a variable
Dim srs As Series
If TypeName(Selection) = "Series" Then
Set srs = Selection
MsgBox "Please select a Chart Series before continuing"
Is A Table Selected?
'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"
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.
'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"
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.
How Do I Modify This To Fit My Specific Needs?
Chances are this post did not give you the exact answer you were looking for. We all have different situations and it's impossible to account for every particular need one might have. That's why I want to share with you: My Guide to Getting the Solution to your Problems FAST! In this article, I explain the best strategies I have come up with over the years to getting quick answers to complex problems in Excel, PowerPoint, VBA, you name it!
I highly recommend that you check this guide out before asking me or anyone else in the comments section to solve your specific problem. I can guarantee 9 times out of 10, one of my strategies will get you the answer(s) you are needing faster than it will take me to get back to you with a possible solution. I try my best to help everyone out, but sometimes I don't have time to fit everyone's questions in (there never seem to be quite enough hours in the day!).
I wish you the best of luck and I hope this tutorial gets you heading in the right direction!
Founder of TheSpreadsheetGuru