Worksheet_Selectionchange problem

Wild Bill

Board Regular
Joined
Feb 20, 2006
Messages
125
I have written a simple macro that gives the user a message when he selects a cell that shouldn't be changed.

Code:
Private Sub Worksheet_Selectionchange(ByVal target As Range)
 If target.Column = 19 And target.Count = 1 And target.Value = "EP DLS" Then
 MsgBox ("Do not change EP DLS to EP or Mat'l DLS.")
 End If
End Sub

It works fine when the user selects a single cell that passes the IF tests and does nothing when the user selects a single cell that doesn't pass the IF tests.

An error occurs when the user selects a multi-cell range. The error message is "Run-time error 13. Type mismatch." Choosing the Debug option takes me to the IF statement.

The tests on target.column and target.count both fail so the problem must be with target.value. Debug mode shows values for target.column and target.count but nothing for target.value. Maybe target.value is undefined for a multi-cell range. How do I deal with this?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

patrickmuldoon99

Active Member
Joined
Jun 27, 2006
Messages
345
simplest solution would be to either say:

cells(target.row, target.col) = "whatever you want"

and place the target.count as an external if statement, so that the counting logic must be passed through first before any other checks are made.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
This will check all the cells in the selection:

Code:
Private Sub Worksheet_Selectionchange(ByVal Target As Range) 
   Dim Cell as Range
   For Each Cell in Target.Cells
      If Cell.Column = 19 And Cell.Value = "EP DLS" Then 
         MsgBox ("Do not change EP DLS to EP or Mat'l DLS.") 
         Exit For
      End If 
   Next Cell
End Sub
 

Forum statistics

Threads
1,136,586
Messages
5,676,671
Members
419,642
Latest member
Hyakkivn

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
Top