VBA macro to count & highlight non-unicode cells

Dimitris254

Board Regular
Joined
Apr 25, 2016
Messages
139
here's my problem:

i work with an excel file that has cells primarily in English, however some of them are in Chinese or other non-Unicode (or whatever is the default Excel encoding) characters.

I would like a macro to change the font color of the cells with such characters, and maybe in the end also mentioning the total number of those cells.
 
Last edited:
Can you post the final solution here? Only excel forum members can view what is posted inside code tags there.
I wonder what was used: regular expressions, the like command, the instr function or something else…
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
for sure!

code provided by jindon:

Rich (BB code):
Sub highlight_nonascii()<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">
    Dim r As Range
    Dim n As Long
    
    Application.ScreenUpdating = False

    Cells.Interior.ColorIndex = xlNone
    With CreateObject("VBScript.RegExp")
        .Pattern = "[^\x20-\x7F]|\n"
        Cells.Replace vbLf, " ", 2
        For Each r In Range("a1", Cells.SpecialCells(11))
            If .test(r.Value) Then r.Interior.Color = vbRed: n = n + 1
        Next r
    End With
    MsgBox n & " cells were highlighted."
    
    Application.ScreenUpdating = True
    
End Sub
</code>
Cells.Replace vbLf, " ", 2 is to remove carriage return (new lines) characters before highlighting
 
Upvote 0
Because newlines can cause problems in the output CSV afaik and i am aware of where the newlines are and i'd rather not have them highlighted - because i will replace them in end anyway.

The cells affected are usually address or office room, e.g.:

(original record in 1 cell)
Room 1,
Location A,
Country A

(after the macro)
Room 1, Location A, Country A
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,869
Messages
6,127,421
Members
449,382
Latest member
DonnaRisso

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