What I want to do
I have a function that iterates through cells to see if they have an #N/A
error or not. If so, a cell in the same row on another column should be coloured green.
The Problem
I am getting the error Run-time error 13: Type Mismatch
. This happens, as soon as it reaches certain cells which contain text. What I found out so far, is that it seems to only throw this error for cells, that exceed 256 characters.
I am using Excel as part of an Office 365 subscription in 64-bit.
What I tried so far
I tried to ignore this error with On Error GoTo [ErrorLabel]
and simply move on to the next iteration. This for some reason works only once. As soon as there is another cell being checked with more than 256 characters, I get the same error again.
EDIT: maybe it has to do with the function IsNA()
expecting a Variant
data type. I guess, since the cell value is too large for being a string, it is not stored in a variable at all, or it is simply something different than Variant
.
This is part of a bigger module with several procedures. So it should be programmatic. isError
and CVErr
as proposed in the comments did not resolve this issue. Still getting a Type Mismatch
.
The Code
The initial code, that produces the error:
For i = 2 To rowCount
If WorksheetFunction.IsNA(Cells(i, 4)) _
Then Cells(i, 1).Interior.Color = RGB(0, 255, 0)
The code, that I tried to fix this issue. This still throws the error (see section "What I tried so far"):
For i = 2 To rowCount
On Error GoTo Error
'based on following condition
' 1. Name does not exist in previous weeks' sheet, identified by VLOOKUP being #N/A
If WorksheetFunction.IsNA(Cells(i, 4)) _
Then Cells(i, 1).Interior.Color = RGB(0, 255, 0)
Error:
Next i
How To Reproduce
Create a blank sheet with one column containing a few lines of text with varying lengths. As shown below, at least one line should exceed 256 characters to test it.
Insert following code in VBA under Sheet1:
Sub isText()
Dim i As Integer
For i = 2 To 6
If WorksheetFunction.isText(Cells(i, 1)) _
Then Debug.Print "Is Text" _
Else Debug.Print "Is Not A Text"
Next i
End Sub
With this example the error is thrown as soon as it reaches line 6.