Type mismatch

david porter cpl

New Member
Joined
Mar 11, 2022
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I'm in need of advice please. The following code returns a 'type mismatch' error on the line 'If .test(A(i, 1)) Then'

The code was not written by myself, but serves to highlight characters within a string that doesn't follow a pattern. The code has worked for many months previously, but for some reason now consistently fails at this point.

VBA Code:
Dim r As Range, A, e, w, i As Long, s
    Dim mtch As Object, m As Object, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    Set r = Range("h8", Range("h" & Rows.Count).End(xlUp))
    A = r.Value: r.Font.ColorIndex = xlAutomatic
    With CreateObject("VBScript.RegExp")
        .Global = True
        .MultiLine = True
        For i = 1 To UBound(A, 1)
            .Pattern = "^[A-Z]+(?=\d+\b)"
            If .test(A(i, 1)) Then
                s = .Execute(A(i, 1))(0)
                .Pattern = "\S+"
                Set mtch = .Execute(A(i, 1))
                .Pattern = s & "\d+"
                For Each m In mtch
                    If Not .test(m) Then
                        r(i).Characters(m.firstindex + 1, m.Length).Font.Color = vbRed
                    Else
                        If Not dic.exists(m.Value) Then
                            ReDim w(1 To 2, 1 To 1)
                        Else
                            w = dic(m.Value)
                            ReDim Preserve w(1 To 2, 1 To UBound(w, 2) + 1)
                        End If
                        Set w(1, UBound(w, 2)) = r(i)
                        w(2, UBound(w, 2)) = Array(m.firstindex + 1, m.Length)
                        dic(m.Value) = w
                    End If
                Next
            End If
        Next
    End With
    For Each e In dic
        If UBound(dic(e), 2) > 1 Then
            w = dic(e)
            For i = 1 To UBound(w, 2) - 1
                If w(1, i).Address = w(1, i + 1).Address Then
                    w(1, i + 1).Characters(w(2, i + 1)(0), w(2, i + 1)(1)).Font.Color = vbRed
                Else
                    w(1, i).Characters(w(2, i)(0), w(2, i)(1)).Font.Color = vbRed
                End If
            Next
        End If
    Next




Any help would be much appreciated. Many thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If there isn't any data below H8, you would get that error since a would not longer be an array.
 
Upvote 0
If there isn't any data below H8, you would get that error since a would not longer be an array.
Hello @RoryA, if there is nothing below H8 the code will fail at For i = 1 To UBound(A, 1).

It is failing on the RegEx expression, effectively CreateObject("VBScript.RegExp").test(A(i, 1)) .
My regex knowledge is limited any thoughts on why that expression would fail ?
 
Upvote 0
@david porter cpl - when it errors out and that line is highlighted can you paste the below into the immediate window and hit enter.
(If you don't see the immediate window hit Ctrl + G and it should open up)
VBA Code:
? "i: " & i & "  " & A(i,1)

Then paste the result you get back here.
 
Upvote 0
@ Alex Blakenburg

Thanks for the reply. When I follow your instructions I get the following...
1646995387330.png
 
Upvote 0
Good point, Alex.

I'd say most likely issue is an error value in the range.
 
Upvote 0
@ Alex Blakenburg

Thanks for the reply. When I follow your instructions I get the following...
View attachment 59790
Try typing or copying in just
? i + 7

i + 7 is the row no.
So show us what is in that row. To save going back and forward also show the one before and after but highlight the i+7 row.

Per Rory's post you could also try filtering on H and look for any # error (#value, #Ref etc) rows.
 
Upvote 0
Solution
After typing in ? i + 7, it returned 301. I just scrolled further down the column (I didn't check before as they all appeared blank), to find some ref# errors in H301:H320. Deleting these seems to clear the errors I have been getting and the sheet now works as planned. Thank you Rory and Alex for your help, this has fixed the problem. I'll do a little investigation as to how those errors occurred, but for now, I've added range("H301:H400").Value="" to clear unwanted values before the code runs, as those cells aren't used for anything anyway. Thank you again.
 
Upvote 0

Forum statistics

Threads
1,215,230
Messages
6,123,752
Members
449,118
Latest member
kingjet

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