I have this vba in my worksheet. When I click onCoother columns that are not column 9,13…
- User
I have this vba in my worksheet. When I click onCoother columns that are not column 9,13 or 17, I get an error on the line: If Len(Target.Value) = 3 Then 'Check if value in clicked cell has more than 3 letters How do I stop this error? The full code is below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim wsd As Worksheet 'On Error Resume Next If Target.Column = 9 Or 13 Or 17 Then 'Check if clicked cell is in column 9, 13 OR 17 If Len(Target.Value) = 3 Then 'Check if value in clicked cell has more than 3 letters Set wsd = ThisWorkbook.Sheets(Target.Value) 'Set To the sheet named in the clicked cell wsd.Activate End If End If End Sub
- Assistant
You need to use the "Or" operator between each condition in the If statement. Currently, the code is interpreting it as "If Target.Column = 9" being true, and then checking if 13 is true (which it always will be), and then checking if 17 is true (which it always will be).
To fix this, change the line to:
If Target.Column = 9 Or Target.Column = 13 Or Target.Column = 17 Then
This will check if the clicked cell is in column 9, 13 or 17 correctly.