The problem is that the Worksheet function Substitute, which I used in the macro, only allows up to 32767 characters. I replaced it with the VBA Replace function, which is too bad, since it's slower. But here's the updated version. I changed the lines in red:

Code:

Public Primality As String
Public Function NumType(ByVal ntype As String, ByVal loc As Long)
Dim i As Long, j As Long, n As Long, x As Long, p2 As String
If Primality = "" Then Primality = "XP"
ntype = UCase(ntype)
If ntype <> "P" And ntype <> "C" Then
NumType = "Invalid code"
Exit Function
End If
ChkAgain:
p2 = Replace(Primality, ntype, "~", , loc - 1)
x = InStr(p2, ntype)
If x > 0 Then
NumType = x
Exit Function
End If
n = Len(Primality) + 1
If n > 1000000 Then
NumType = "The requested value is over 1000000"
Exit Function
End If
Primality = Primality & String(10000, "P")
For i = 2 To Len(Primality)
If Mid(Primality, i, 1) = "P" Then
x = Int(n / i) * i
x = IIf(x <= i, i * 2, x)
For j = x To Len(Primality) Step i
Mid(Primality, j, 1) = "C"
Next j
End If
Next i
GoTo ChkAgain:
End Function

## Like this thread? Share it with others