Selection Change event

51DegreesNorth

New Member
Joined
Mar 29, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'd like to keep an eye an a certain range: if the contents of one or more of the cells in that range changes, the selection change event must call a subroutine to perform some actions.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)


VBA Code:
 If Intersect(Target, Range("A1:A6")) Is Nothing Then
    Exit Sub
 End If
 ' but if there is an overlap:
    Range("E5").Select ' as an example, I'll replace it with Call Some_sub later.
 End Sub

This code works, it selects E5 when the selection change event occurs. That's not what I want. Suppose there's a number in A3 which must be altered or deleted. Click on the cell and you end up at E5 before you had a change to edit or delete A3. The above code is on sheet level. The VBA Project contains no mudules and the workbook is otherwise completely empty.

I'm new to this whole 'events' thing ... Can you point me in the right direction please ? Is this even the correct approach for what I'm trying to do ?

Thank you !
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I'm not understanding. If you don't want to go to E5 then why not just replace that line with what you do want? What you have basically says "If something other than A1 to A6 was selected, do nothing - exit the procedure. Any code outside of that IF block will execute, which is why you go to E5 (which btw will run the event again).

Maybe you're saying you don't just want an event that fires every time you choose a different cell? Could be that you want the worksheetChange event.
 
Upvote 0
Sounds like you should be using a change event & not a selection change
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
Upvote 0
Solution
Thank you both.
@ Micron, the E5 thing is just a placeholder. I will call the sub once this piece of code does what I want, which is the trigger to fire after the contents of a cell has changed. Now it seems to fire on clicking/selecting of the cell, and that's too early. Users won't be able to make changes to the cell. The sub looks at the kind of change made and takes it from there.
@ Fluff - I've assumed (assumptions, always dangerous) that "watching" a range of 50 or so cells takes far less time than watching the whole sheet.
 
Upvote 0
Change event won't detect that 5 was replaced with 5 (AFAIK) so if that matters to you, use a module level variable like this:

Option Explicit
Dim CellValue As Variant

Then when selection changes, set the variable value:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rPrintArea As Range

 If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
   CellValue = Target
 End If
 
 End Sub

Then when an entry is made, see if the new value is the same as the old value:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Application.Intersect(Range("A1:A10"), Range(Target.Address)) Is Nothing Then
   If Target <> CellValue Then
      MsgBox "Cell " & Target.Address & " has changed."
   End If
End If

End Sub
Otherwise, forget the variable and use only the change event, I guess. Not sure what range, if any, that you want to be concerned with.
 
Upvote 0
You can still "watch" certain cells, just a change event only triggers when you physically change the value in a cell, whereas the SelectionChange triggers whenever you select a cell
 
Upvote 0
If you are only changing a single cell you can use something like
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Range("A1:A6")) Is Nothing Then
      Range("E5").Select ' as an example, I'll replace it with Call Some_sub later.
   End If
End Sub
 
Upvote 0
Some quick testing later .... everything works fine now, even with cells with data validation. I have a much clearer understanding of the difference between the two events now.
Thank you again.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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