簡體   English   中英

Excel隱形問號

[英]Excel invisible question mark

我將從系統中提取的信息提取到Excel文件中。 “Leone”這兩個名字看起來是一樣的,但Excel對此有不同的認識。

塞拉利昂

塞拉利昂

字符串的長度是不一樣的,如果我用VBA檢查值是不可見的? 是第一個角色。

你能幫助我擺脫看不見的角色嗎?

在此輸入圖像描述

要擺脫所有看不見的東西? 你可以試試這個。

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

這里得到這個。

一般來說這很奇怪 - 這就是chrome從問題中呈現HTML的方式:

在此輸入圖像描述

這是一種解決方法,它檢查字符串的字符並構建一個新的字符串,如果其中一個等於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

如果文本來自復制/粘貼,則可能包含其他一些不可打印的字符。 這些可能會在VBA編輯器中顯示為? 這通常是字體不支持時呈現unicode字符的方式。

我會在其中一個單元格中嘗試使用公式=CODE(LEFT(A3,1))來查看隱形字符的Unicode代碼點是什么。

如果結果是非ascii聊天,那么你可以編寫一個宏來根據代碼值去掉有問題的字符。

要從您的范圍的所有單元格中刪除多次出現的非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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM