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

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
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

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