Two things I think you might find interesting. First, your IsAllASCII can
be accomplished with a one-liner routine (although the function's
argument is changed to a String value)...
Function IsAllASCII(S As String) As Boolean
IsAllASCII = Not x Like "*[!" & Chr(0) & "-" & Chr(127) & "]*"
End Function
Second, because the function is a one-liner, we can eliminate the
function call altogether by incorporating the one-liner inside the Change
event code; plus we can change your logic to simplify the Change event
code considerably. This event code procedure functions the same as your
originally posted code...
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cel As Range
For Each Cel In Target
If Cel.Value Like "*[!" & Chr(0) & "-" & Chr(127) & "]*" Then
Cel.Interior.ColorIndex = 38
Else
Cel.Interior.ColorIndex = xlNone
End If
Next
End Sub
In the above form, the function color assignment is obvious (even if the
If..Then statement might cause one to pause<g>); here is even shorter,
but more obfuscated, code that functions identically to the above...
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cel As Range
For Each Cel In Target
Cel.Interior.ColorIndex = xlNone - 4180 * (Cel.Value Like "*[!" & _
Chr(0) & "-" & Chr(127) & "]*")
Next
End Sub
In case you were wondering where the 4180 came from, it is formed by
subtracting 38 (the color index value you want to assign for non-ASCII
text) from the predefined xlNone constant.
--
Rick (MVP - Excel)
Peter T said:
What you now say you want is very different to what you asked in your
OP
this is only lightly tested -
Private Sub Worksheet_Change(ByVal Target As Range)
Dim bIsASCII As Boolean
Dim nClr1 As Long, nClr2 As Long
Dim ba() As Byte
Dim cel As Range
For Each cel In Target
ba = CStr(cel.Value)
If IsNumeric(cel) Then
bIsASCII = True
Else
bIsASCII = IsAllASCII(ba)
End If
With cel.Interior
nClr1 = .ColorIndex
nClr2 = 0
If bIsASCII Then
If nClr1 = 38 Then nClr2 = xlNone
Else
If nClr1 <> 38 Then nClr2 = 38
End If
If nClr2 Then .ColorIndex = nClr2
End With
Next
End Sub
Function IsAllASCII(ba() As Byte) As Boolean
Dim bFlag As Boolean
Dim i As Long
For i = 0 To UBound(ba) - 1 Step 2
If ba(i) < 128 And ba(i + 1) = 0 Then
' it's an ASCII
Else
bFlag = True
Exit For
End If
Next
IsAllASCII = Not bFlag
End Function
Regards,
Peter T
Hi,
I just want to high light the cell with pink. I have about 30 000 rows
and
30 columns in use.
This is to be used as data is entered to cell. Sub
Worksheet_Change(ByVal
Target As Range)
BR
MakeLei
:
So are you saying you want to check if any text cells contain
characters in
the range chr(x) where x is between 128 and 255
Also, could any formula cells return text with these characters.
For the result do you just want a yes/no answer or any information
about
which cells contain non ASCII characte5rs
Regards,
Peter T
Hi,
xls and 2003
BR
MakeLei
:
what sort of file, xls, txt....
Regards,
Peter T
Hi,
What would be the possibilities to check that only ASCII chars
are used
within one file?
Thanks in advance once again
Markku