Run-time error '13': Type mismatch - selecting multiple cells

GuillaumeWiatr

New Member
Joined
May 1, 2012
Messages
2
Hi there,

I am using the following macro to have the letter u appear when the user clicks on cells in columns 5,6,7 and 8. This works line by line. I only want it to work when the line corresponds to a specific type of item, specified by the letter "S" in another column. Te macro works well.

But here is my problem: When the user selecst more than one cell a a time, anywhere in the worksheet, I get a "Run-time error '13': Type mismatch message". The debugger points at the line 4 (If Target.Column = 5 And Range(Cellule).Offset(0, 13).Value = "S" Then).

I am thinking that my variable must not have the right type. Maybe the offset function is not appropriate. Being new to VBA development and after researching for a while, I think I need you help please!

------------------------

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim Cellule As String
Cellule = Target.Address

If Target.Column = 5 And Range(Cellule).Offset(0, 13).Value = "S" Then
Range(Cellule).Value = "u"
Range(Cellule).Offset(0, 1).Value = ""
Range(Cellule).Offset(0, 2).Value = ""
Range(Cellule).Offset(0, 3).Value = ""
Else
If Target.Column = 6 And Range(Cellule).Offset(0, 12).Value = "S" Then
Range(Cellule).Value = "u"
Range(Cellule).Offset(0, -1).Value = ""
Range(Cellule).Offset(0, 1).Value = ""
Range(Cellule).Offset(0, 2).Value = ""
Else
If Target.Column = 7 And Range(Cellule).Offset(0, 11).Value = "S" Then
Range(Cellule).Value = "u"
Range(Cellule).Offset(0, -2).Value = ""
Range(Cellule).Offset(0, -1).Value = ""
Range(Cellule).Offset(0, 1).Value = ""
Else
If Target.Column = 8 And Range(Cellule).Offset(0, 10).Value = "S" Then
Range(Cellule).Value = "u"
Range(Cellule).Offset(0, -3).Value = ""
Range(Cellule).Offset(0, -2).Value = ""
Range(Cellule).Offset(0, -1).Value = ""
Else


End If
End If
End If
End If
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim Cellule As String
If Target.Count > 1 Then Exit Sub
Cellule = Target.Address
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,249
Members
449,075
Latest member
staticfluids

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