0
votes

The following code fires every time a user exits a excel cell. It then checks for the cell value matching a regex expression. This works.

If a user selects a range of cells and perhaps deletes the contents - I get a VBA error Type 13 mismatch. How can I put a test in so that the user does not get a error.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal target As Range)
Dim strPattern As String: strPattern = "^[a-z]{2,4}[0-9]{2,4}$"
Dim regEx As New RegExp
Dim strInput As String
Dim Myrange As Range

Set Myrange = ActiveSheet.Range("A1")

If target.Value <> "" Then
    strInput = LCase(target.Value)
    With regEx

This line throws the type 13 error:

    If target.Value <> "" Then

It needs to incorporate a check that target.value is a cell rather than a range before progressing.

1

1 Answers

1
votes

The .Value of a multi-cell range is a 2D Variant array, so it can't be compared to "".

One fix is to add

If Target.CountLarge > 1 Then Exit Sub