VBA:如何从数据

时间:2017-01-26 20:36:08

标签: string vba replace ms-access-2007

我需要以编程方式删除不可打印的字符,例如:

tabs - char(9) 换行符 - char(10) 回车 - char(13) 数据链接转义 - char(16)

我启动了一个通用函数,它将从ms访问表单字段的lost_focus事件中调用。

我还没弄清楚如何识别字符串何时包含不需要的字符。

    Function RemoveNonPrintableCharacters(ByVal TextData) As String

        Dim dirtyString As String
        Dim cleanString As String
        Dim iPosition As Integer

        If IsNull(TextData) Then
            Exit Function
        End If

        dirtyString = TextData
        cleanString = ""

        For iPosition = 1 To Len(dirtyString)
            Select Case Asc(Mid(dirtyString, iPosition, 1))
                Case 9    ' Char(9)
                Case 10   ' Char(10)
                Case 13   ' Char(13)
                Case 16   ' Char(16)
                Case Else ' Add character to clean field.
                    cleanString = cleanString & Mid(dirtyString, iPosition, 1)
            End Select
        Next

        RemoveNonPrintableCharacters = cleanString

    End Function

这些是我在测试时使用的2个字符串:

This line,    has       multiple,     tabs       that   need to be removed


This line, has multiple,     
line
breaks
that
need to be removed

This line,    has       multiple,     tabs       that   need to be removed
And
Also contains
multiple,     
line
breaks
that
need to be  removed

9 个答案:

答案 0 :(得分:2)

这是当我搜索要使用的快速功能时的最高google结果,我有一个很好的旧google,但是真正解决不了我的问题的任何事情都没有出现。

主要问题是,即使没有问题,所有这些函数也会触及原始字符串。放慢速度。

我已对其进行了重写,以便仅在字符不好的情况下才进行修改,并且还会扩展到所有不可打印的字符以及标准ascii以外的字符。

Public Function Clean_NonPrintableCharacters(Str As String) As String

    'Removes non-printable characters from a string

    Dim cleanString As String
    Dim i As Integer

    cleanString = Str

    For i = Len(cleanString) To 1 Step -1
        'Debug.Print Asc(Mid(Str, i, 1))

        Select Case Asc(Mid(Str, i, 1))
            Case 1 To 31, Is >= 127
                'Bad stuff
                'https://www.ionos.com/digitalguide/server/know-how/ascii-codes-overview-of-all-characters-on-the-ascii-table/
                cleanString = Left(cleanString, i - 1) & Mid(cleanString, i + 1)

            Case Else
                'Keep

        End Select
    Next i

    Clean_NonPrintableCharacters = cleanString

End Function

答案 1 :(得分:1)

A = Chr(09) & "Cat" & Chr(10) & vbcrlf

A = Replace(A, Chr(10))
A = Replace(A, Chr(13))
A = Replace(A, Chr(09))

Msgbox A

这就是人们通常的做法。

您的代码正在创建许多隐式变量。

答案 2 :(得分:0)

    Function RemoveNonPrintableCharacters(ByVal TextData) As String

    Dim dirtyString As String
    Dim cleanString As String
    Dim iPosition As Integer

    If IsNull(TextData) Then
        Exit Function
    End If

    dirtyString = TextData
    cleanString = ""

    For iPosition = 1 To Len(dirtyString)
        Select Case Asc(Mid(dirtyString, iPosition, 1))
            Case 9, 10, 13, 16
                cleanString = cleanString & " "
                Case Else
                cleanString = cleanString & Mid(dirtyString, iPosition, 1)
        End Select
    Next

    RemoveNonPrintableCharacters = cleanString

End Function

答案 3 :(得分:0)

'首先你需要找一个角色

YourStr =“Bla bla bla ......”

if instr(YourStr,chr(10))> 0然后

NewStr = Replace(YourStr, Chr(10),"")

结束如果

答案 4 :(得分:0)

我正在使用空格字符chr(32)替换不可打印的字符,但您可以根据需要对其进行更改。

Function RemoveNonPrintableCharacters(ByVal TextData) As String
Dim sClean$

sClean = Replace(TextData, Chr(9), Chr(32))
sClean = Replace(sClean, Chr(10), Chr(32))
sClean = Replace(sClean, Chr(13), Chr(32))
sClean = Replace(sClean, Chr(16), Chr(32))

RemoveNonPrintableCharacters = sClean

End Function

答案 5 :(得分:0)

这只能从字符串的右侧删除非打印字符,而不用空格替换字符。

Function fRemoveNonPrintableCharacters(ByVal TextData) As String
Dim dirtyString As String
Dim cleanString As String
Dim iPosition As Integer

If IsNull(TextData) Then
    Exit Function
End If

dirtyString = TextData
cleanString = ""

For iPosition = Len(dirtyString) To 1 Step -1
    Select Case Asc(Mid(dirtyString, iPosition, 1))
        Case 9, 10, 13, 16, 32, 160
            cleanString = cleanString
            Case Else
            cleanString = Left(dirtyString, iPosition)
            Exit For

    End Select
Next

fRemoveNonPrintableCharacters = cleanString

End Function

答案 6 :(得分:0)

使用Excel Clean函数似乎应该更简单。以下内容也适用:

myString = Worksheets("Sheet1").Range("A" & tRow).Value 
myString = Application.WorksheetFunction.Clean(myString)

您还可以使用其他普通的和自行开发的Excel功能:

myString = Application.WorksheetFunction.Trim(myString)

仍然没有让Substitute函数以这种方式工作,但是我正在努力。

答案 7 :(得分:0)

当出现 Unicode 字符时,应修改此处显示的代码。我的建议包括程序无法识别的字符:

Public Function Clean_NonPrintableCharacters(Str As String) As String

    'Removes non-printable characters from a string

    Dim cleanString As String
    Dim i As Integer

    cleanString = Str
    
    For i = Len(cleanString) To 1 Step -1

        If Chr(Asc(Mid(cleanString, i, 1))) <> Mid(cleanString, i, 1) Then
        cleanString = Left(cleanString, i - 1) & Mid(cleanString, i + 1)
        End If
        
    Next i

    Clean_NonPrintableCharacters = WorksheetFunction.Clean(cleanString)

End Function  

答案 8 :(得分:0)

可以通过RegEx解决(在Tools - References in VBE中添加MS VBScript正则表达式):

Function NormalString(text As String, Optional filler = vbNullString) As String
    Dim re As New RegExp
    With re
        .Pattern = "([\x00-\x1F\xA0])"
        .Global = True
        text = .Replace(text, filler)
    End With
    NormalString = text
End Function