Excel隐形问号

时间:2017-10-13 07:24:22

标签: excel excel-vba vba

我将系统中提取的信息提取到Excel文件中。 名字" Leone"看起来是一样的,但Excel会以不同的方式识别它。

字符串的长度不一样,如果用VBA检查值是不可见的?是第一个角色。

你能帮助我摆脱看不见的角色吗?

enter image description here

4 个答案:

答案 0 :(得分:2)

要摆脱所有不可见的?,你可以试试这个。

Sub CleanUnicode()
    Dim n As Long, strClean As String, strChr As String
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Sheet3")  'change Sheet3 to data sheet
    For Each cel In ws.Range("A1:A10")      'change A1:A10 to working range
        strClean = cel.Value
        For n = Len(strClean) To 1 Step -1
            strChr = Mid(strClean, n, 1)
            If AscW(strChr) = 8203 Then     '? is unicode character 8203
                strClean = Replace(strClean, strChr, "")
            End If
        Next
        cel.Value = WorksheetFunction.Trim(strClean)
    Next cel
End Sub

您也可以使用If AscW(strChr) = 8203 Then而不是If AscW(strChr) > 255 Then

编辑1:根据@ YowE3K的建议。假设您在要替换的单元格中只有Unicode 8203

Sub CleanUnicode()
    Dim n As Long, strClean As String, strChr As String
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Sheet3")  'change Sheet3 to data sheet
    For Each cel In ws.Range("A1:A10")      'change A1:A10 to working range
        cel.Value = Replace(cel.Value, ChrW(8203), "")
    Next cel
End Sub

来自here

答案 1 :(得分:1)

一般来说这很奇怪 - 这就是chrome从问题中呈现HTML的方式:

enter image description here

这是一种解决方法,它检查字符串的字符并构建一个新字符串,如果其中一个等于63.非常像一个简单的替换函数:

Public Function removeInvisible(rngRange As Range) As String

    Dim cnt As Long

    For cnt = 1 To Len(rngRange)
        If AscW(Mid(rngRange, cnt, 1)) <> 8203 Then
            removeInvisible = removeInvisible & Mid(rngRange, cnt, 1)
        End If
    Next cnt

End Function

答案 2 :(得分:0)

如果文本来自复制/粘贴,则可能包含其他一些不可打印的字符。 这些可能会在VBA编辑器中显示为?这通常是字体不支持时呈现unicode字符的方式。

我会试试这个公式 在其中一个单元格中=CODE(LEFT(A3,1))查看隐形字符的Unicode代码点是什么。

如果结果是非ascii聊天,那么您可以编写一个宏来根据代码值去掉有问题的字符。

答案 3 :(得分:0)

要从您的范围的所有单元格中删除多次出现的非ascii字符,您可以使用此字符。

Option Explicit

Sub test()
    Dim regEx As Object
    Dim temparray() As String
    Dim myrange As Range
    Dim lrow As Long
    Dim lcol As Long
    Dim counter As Long
    Dim i As Long
    Dim j As Long

    Set regEx = CreateObject("vbscript.regexp")

    With regEx
        .Pattern = "[^\u0000-\u007F]"
        .MultiLine = False
        .Global = True
        .IgnoreCase = False
    End With
    'set your last row and column
    lrow = 5
    lcol = 5

    ReDim temparray(1 To lrow, 1 To lcol)
    Set myrange = Sheets("Sheet1").Range(Cells(1, 1), Cells(lrow, lcol))

    Application.ScreenUpdating = False

    counter = 0

    For i = 1 To lrow
        For j = 1 To lcol
            temparray(i, j) = regEx.Replace(myrange.Cells(i, j).Value, "")
          counter = counter + 1
        Next j
    Next i

    myrange.Value = temparray

    Application.ScreenUpdating = True
End Sub