Event when multiple cells selected

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,900
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have the following code:

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    MsgBox "Row " & Target.Row & " has been changed."
    
End Sub

If I select cell A1 and pressed delete, it correctly shows the message:

Code:
Row 1 has been changed

However, when I selected multiple cells, for example, A1 to A3, then pressed delete, the message still only shows:

Code:
Row 1 has been changed

What can I do to make the message box show:

Code:
Row 1:3 has been changed

or perhaps make the message box appear three times:

Code:
Row 1 has been changed

and

Code:
Row 2 has been changed

and

Code:
Row 3 has been changed

Thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You could just use Target.Address and change your text:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then
    MsgBox "Cells " & Target.Address & " have been changed."
Else
    MsgBox "Cell " & Target.Address & " has been changed."
End If
End Sub

I tried other options to just display the row, but it was either only showing the first row in the selection, or the entire selection.
 
Upvote 0
You could just use Target.Address and change your text:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then
    MsgBox "Cells " & Target.Address & " have been changed."
Else
    MsgBox "Cell " & Target.Address & " has been changed."
End If
End Sub

I tried other options to just display the row, but it was either only showing the first row in the selection, or the entire selection.
Thanks.

I managed to work it out.

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
   
    Dim rng As Range

    For Each rng In Target
 
        MsgBox "Row " & rng.Row & " has been changed."
    
    Next rng

End Sub
 
Upvote 0
Thanks.

I managed to work it out.

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
  
    Dim rng As Range

    For Each rng In Target
 
        MsgBox "Row " & rng.Row & " has been changed."
   
    Next rng

End Sub
Interesting that you got that working. That was one of the things I tried, and it kept giving me the first row in the range instead of each different row. Then again, as I am typing this, I think I was using "MsgBox Target.Row" in the loop instead of "MsgBox rng.Row".
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

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