简体   繁体   中英

hidden space in excel

I tried almost all the methods (CLEAN,TRIM,SUBSTITUTE) trying to remove the character hiding in the beginning and the end of a text. In my case, I downloaded the bill of material report from oracle ERP and found that the item codes are a victim of hidden characters.

After so many findings, I was able to trace which character is hidden and found out that it's a question mark'?' (via VBA code in another thread) both at the front and the end. You can take this item code‭: ‭11301-21‬

If you paste the above into your excel and see its length =LEN(), you can understand my problem much better.

I need a good solution for this problem. Therefore please help!

Thank you very much in advance.

Thanks to Gary's Student , because his answer inspired me.

Also, I used this answer for this code.

This function will clean every single char of your data, so it should work for you. You need 2 functions: 1 to clean the Unicode chars, and other one to clean your item codes_

        Public Function CLEAN_ITEM_CODE(ByRef ThisCell As Range) As String
    If ThisCell.Count > 1 Or ThisCell.Count < 1 Then
        CLEAN_ITEM_CODE = "Only single cells allowed"
        Exit Function
    End If


    Dim ZZ As Byte

    For ZZ = 1 To Len(ThisCell.Value) Step 1
        CLEAN_ITEM_CODE = CLEAN_ITEM_CODE & GetStrippedText(Mid(ThisCell.Value, ZZ, 1))
    Next ZZ


    End Function

    Private Function GetStrippedText(txt As String) As String
If txt = "–" Then
    GetStrippedText = "–"
Else
    Dim regEx As Object

    Set regEx = CreateObject("vbscript.regexp")
    regEx.Pattern = "[^\u0000-\u007F]"
    GetStrippedText = regEx.Replace(txt, "")
End If

End Function

And this is what i get using it as formula in Excel. Note the difference in the Len of strings:

在此处输入图片说明

Hope this helps

You have characters that look like a space character, but are not. They are UniCode 8236 & 8237.

Just replace them with a space character (ASCII 32) .

EDIT#1:

Based on the string in your post, the following VBA macro will replace UniCode characters 8236 amd 8237 with simple space characters:

Sub Kleanup()
Dim N1 As Long, N2 As Long
Dim Bad1 As String, Bad2 As String

N1 = 8237
Bad1 = ChrW(N1)
N2 = 8236
Bad2 = ChrW(N2)

Cells.Replace what:=Bad1, replacement:=" ", lookat:=xlPart
Cells.Replace what:=Bad2, replacement:=" ", lookat:=xlPart

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM