Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 改进清除非打印字符和其他ASCII字符的功能_Excel_Excel 2013_Vba - Fatal编程技术网

Excel 改进清除非打印字符和其他ASCII字符的功能

Excel 改进清除非打印字符和其他ASCII字符的功能,excel,excel-2013,vba,Excel,Excel 2013,Vba,我有一份Excel 2013工作簿,其中包含从各种来源导入的工作表 这些文件中到处都包含Unicode字符,而clean函数没有考虑这些字符 我发现一个函数可以一个单元格一个单元格地工作,但我希望它能在一系列单元格上使用,而不是将函数单独放在每个单元格中 有人能帮我转换这个函数吗 多谢各位 Function CleanTrim(ByVal S As String, Optional ConvertNonBreakingSpace As Boolean = True) As String

我有一份Excel 2013工作簿,其中包含从各种来源导入的工作表

这些文件中到处都包含Unicode字符,而clean函数没有考虑这些字符

我发现一个函数可以一个单元格一个单元格地工作,但我希望它能在一系列单元格上使用,而不是将函数单独放在每个单元格中

有人能帮我转换这个函数吗

多谢各位

  Function CleanTrim(ByVal S As String, Optional ConvertNonBreakingSpace As Boolean = True) As String
  Dim X As Long, CodesToClean As Variant
  CodesToClean = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
                       21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 129, 141, 143, 144, 157)

  If ConvertNonBreakingSpace Then S = Replace(S, Chr(160), " ")
  For X = LBound(CodesToClean) To UBound(CodesToClean)
    If InStr(S, Chr(CodesToClean(X))) Then S = Replace(S, Chr(CodesToClean(X)), "")
  Next
  CleanTrim = WorksheetFunction.Trim(S)

'Call function == use CleanTrim just like it was a built-in Excel function. For example, =CleanTrim(B2)
End Function

下面是我编写的子过程,它在测试时起作用

Sub CleanCells()
  Dim x As Long, CodesToClean As Variant
  CodesToClean = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
                       21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 129, 141, 143, 144, 157)

  Dim rng As Range
    Dim row As Range
    Dim cell As Range

    Set rng = Range("A1:A168")

    For Each row In rng.Rows
        For Each cell In row.Cells
        'Do Something
                S = Replace(S, Chr(160), " ")
                For x = LBound(CodesToClean) To UBound(CodesToClean)
                  If InStr(S, Chr(CodesToClean(x))) Then S = Replace(S, Chr(CodesToClean(x)), "")
                Next
                WorksheetFunction.Trim (S)

        Next cell
    Next row

End Sub

Regexp
将更干净

作为下面的自定义项(可以在数组中使用)


它需要是一个子函数,不能像工作表上的公式那样调用。另一方面,此函数不能清除Unicode。它从一个字符串中删除不可打印的字符和更多的ASCII字符,这样做效率很低。单元格在导入的工作表上是否位于同一位置?如果存在一致性,则可以编写sub来调用单元格上的函数。您想在什么范围内使用它?您可以将整个范围作为一个整体,查看
范围。替换

Function strClean(strIn As String) As String
Dim objRegexp As Object
Set objRegexp = CreateObject("vbscript.regexp")
With objRegexp
    'hex codes for 0-31, 127, 129, 141, 143, 144, 157
    .Pattern = "[\x00-\x1F]|\x7F|\x7F|\x81|\x8D|\x8F|\x90|\x9D"
    .Global = True
    strClean = .Replace(strIn, vbNullString)
End With
End Function