Click on cell to trigger a macro:

sooshil

Board Regular
Joined
Feb 21, 2013
Messages
104
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have a table and I want to trigger a macro when I click on values in a specific column of that table.
I found a piece of code online to put as the worksheet code to accomplish this. The code is as follows:
VBA Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim myRng
    myRng = Selection.Address
  
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range(myRng)) Is Nothing Then
            Call SelectEmpRow
        End If
    End If
End Sub

The SelectEmpRow is the macro I want to run when a cell in the specific column of the table is clicked.
Now, I am having a problem. The code calls that macro anywhere in the sheet is clicked. I want the desired click area to be one specific column of the table.
What can we edit in the code to accomplish my requirement?
Sheet name : Sheet1
Table name : Table1
Column number : 5 (Where I want to click to trigger macro)
Column header : EmpName

Thank you.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Maybe this ...UNTESTED
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   If Target.Column <> 5 Then Exit Sub
If Not Intersect(Target, Range("Table1")) Is Nothing Then
     Call SelectEmpRow
End If
End Sub
 
Upvote 0
Shorter
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("Table1")) Is Nothing Then Call SelectEmpRow
End Sub
 
Upvote 0
Another option
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Me.ListObjects("Table1").ListColumns(5).DataBodyRange) Is Nothing Then
      Call SelectEmpRow
   End If
End Sub
This will work on the 5th column of your table.
 
Upvote 0
Thank you Michael M

Your first solution worked but second didn't. If I am not wrong, the second code is not specifying which column is the target column. Any way, I now have multiple solution to choose from. Thank you again.
 
Upvote 0
Glad WE could help.....and you manged to get a solution...(y)(y)
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top