Reputation: 37
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.
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.
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 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
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.
Upvotes: 1
Views: 587
Reputation: 37
Thanks to the commenters, the issue is resolved.
the reason for this error likely has to do with the WorksheetFunctions.isNA()
function. According to the documentation of this function, it is expecting a parameter of type Variant
. But in my excel version (office 365 (16.0.12527.21096) 64-bit) any cell with strings containing more than 255 characters it is stored internally as an Array
data type. This was checked with the =TYPE
Excel function. It returns 64
to imply an array (documentation).
Instead of using Cells(rowNum, colNum)
I used Cells(rowNum, colNum).Value
which returns a Variant
data type, not an Array
. This is exactly the type, that I needed.
Upvotes: 1
Reputation: 152660
using:
and this code:
Sub isText()
Dim str As Variant
Dim i As Integer
For i = 2 To 6
str = Cells(i, 1)
On Error Resume Next
str = Left(str, 255)
On Error GoTo 0
If Not IsError(str) Then
Debug.Print "Is Text"
Else
Debug.Print "Is Not A Text"
End If
Next i
End Sub
I get:
Upvotes: 0
Reputation: 2529
It seems to me that you are in vain going through all the cells in search of erroneous formulas. Excel can give you all the errors with one operator and you can only iterate over them. Hopefully, in these cells you will not find texts longer than 4 or 5 characters:
Sub FindErrorsOnActiveSheet()
Dim ws As Worksheet
Dim aErrors As Range
Dim oCell As Range
Set ws = ActiveSheet ' or any other
Set aErrors = ws.Cells.SpecialCells(xlCellTypeFormulas, 16)
For Each oCell In aErrors
Debug.Print "Cell " & oCell.Address(False, False, xlA1) & " has error " & oCell.Text
If oCell.Text = "#N/A" Then oCell.Offset(0, 1 - oCell.Column).Interior.Color = RGB(0, 255, 0)
Next oCell
End Sub
Upvotes: 0