Worksheet_Change for multiple cells

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This displays the value of a changed cell on the worksheet:


Code:
Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)
    
    MsgBox Target.Value
    
End Sub


However if multiple cells are selected, an error message pops up:


Code:
Run-time error '13':

Type mismatch


I can suppress this by disallowing multiple cells to be selected, such as:


Code:
If Selection.Count > 1 Then


but is there a way to actually make the code work when multiple cells are selected?


Thanks
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try
Code:
    MsgBox Target[COLOR=#ff0000](1)[/COLOR].Value

Thanks but that only displays the first value.

For example, if I cells A1 and B1 contain the values a and b and then I copy both and paste into cell C1, the message box only displays a and not b.

I suppose adapting your code, I would have to somehow note how many cells were selected, then loop to display each one.
 
Last edited:
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Cl As Range
   Dim Msg As String
   
   For Each Cl In Target
      Msg = Msg & vbLf & Cl.Value
   Next Cl
   MsgBox Msg
End Sub
 
Upvote 0
Similar to Fluff's solution but without the initial Line Feed:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Msg, i As Long
    Msg = Target(1).Value
    For i = 2 To Target.Count
        Msg = Msg & vbCrLf & Target(i).Value
    Next
    MsgBox Msg
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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