Home » excel » excel – VBA if a specific columns and rows are selected, Type Mismatch error

excel – VBA if a specific columns and rows are selected, Type Mismatch error

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to write a code where sub activates when I select a specific column and specific row but gives me a type mismatch error on the following code:

If Selection.Worksheet.Columns("E:L") And Selection.Worksheet.Rows("16:99999") Then
How to&Answers:

You will see that the keyword Target always returns the Range Object of the cell that fired the Worksheet_Change Event. So, armed with this knowledge we can now use the Intersect Method to take some action when/if specified cell is changed. For example;

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("E12:L54787")) Is Nothing Then
        MsgBox "Hello"
    End If
End Sub

Answer:

Write the code in the corresponding worksheet:

enter image description here

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If (Not Intersect(Target, Columns("E:L")) Is Nothing) And _
        (Not Intersect(Target, Rows("16:99999")) Is Nothing) Then

        Debug.Print Target.Address

    End If

End Sub