In VBA, trigger a sub when a cell gets the focus

batman37

New Member
Joined
Jul 21, 2007
Messages
14
I want to be able to trigger a subroutine when I click on a certain cell. Presumably clicking on any cell would trigger the sub; but I can put code in the sub to determine if the selected cell meets my criteria for action, and if yes, what that action should be. So far I haven't figured out how to trigger a sub on cell selection, or even if it's possible.

Thanks in advance for any example or words of advice.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Right click the sheet tab, press view code and paste in:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Address(0, 0) = "A1" Then
    MsgBox "A1 selected"
End If

End Sub
 
Upvote 0
Thanks Steve, that did just what I want. With the code below, clicking on a stock symbol in col B toggles a control character ("x") in another column.
I apologize if I didn't apply the code tags correctly.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Created 11/07/18 v08 adapted from example in MrExcel forum
'Purpose When a stock symbol (in B) is selected, toggle the mark in col H that
'        turns on and off the display of $Value & $Prof/Loss in cols J & K.
'        The mark can be anything; in this instance it's chosen to be "x".

Set StockData = Range("tData_Sort")     'Trial!B5:K54
Dim CellRow As Integer                  'wksh row# of selected cell
If ActiveCell.Column = 2 Then           'limit action to symbols, in B
   CellRow = ActiveCell.Row             'stock's row#
'  Respond only to cells that contain symbols
   If CellRow > 4 And _
      CellRow < WorksheetFunction.CountA(StockData.Columns(2)) Then 'valid symb
      CellRow = CellRow - 4             'adjust wksh row# to range row#
'     Toggle mark in col H on/off
      If StockData(CellRow, 7) = "x" Then
         StockData(CellRow, 7) = ""
      Else
         StockData(CellRow, 7) = "x"
      End If
   End If
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,447
Members
448,898
Latest member
drewmorgan128

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