使用宏从Excel电子表格中删除任何非指定字符

时间:2017-08-09 23:44:00

标签: excel excel-vba character vba

我正在尝试通过删除任何非标准字符来清理Excel中的.CSV文件。我关心的唯一字符是A-Z,0-9和一些标准的标点符号。任何其他字符,我想删除。

当我找到一个包含我没有指定的任何字符的单元格时,我得到了以下宏来删除整行,但我不知道如何让它实际删除字符本身。

Sub Replace()
Dim sCharOK As String, s As String
Dim r As Range, rc As Range
Dim j As Long

sCharOK = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789, `~!@#$%^&*()_+-=[]\{}|;':"",./<>?™®"

Set r = Worksheets("features").UsedRange.SpecialCells(xlCellTypeConstants, xlTextValues)

' loop through all the cells with text constant values and deletes the rows with characters not in sCharOK
For Each rc In r
    s = rc.Value
    For j = 1 To Len(s)
        If InStr(sCharOK, Mid(s, j, 1)) = 0 Then
            rc.EntireRow.Delete
            Exit For
        End If
    Next j
Next rc

End Sub

我认为有一种相当简单的方法可以使这个代码适应这个功能,但是我对VBA不太熟悉,不知道如何去做。任何见解都非常感谢!

4 个答案:

答案 0 :(得分:3)

另一种方式是Range.Replace喜欢:

Sub test()
  Dim sCharOK As String
  sCharOK = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789, `~!@#$%^&*()_+-=[]\{}|;':"",./<>?™®" & Chr(1)
  Dim i As Long
  For i = 0 To 255
    If InStr(sCharOK, Chr(i)) = 0 Then
      ActiveSheet.Cells.Replace What:=Chr(i), Replacement:="", LookAt:=xlPart, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
    End If
  Next
End Sub

修改

查看@ ryguy72答案还提供另一种方法,如果只需要删除不可打印的字符(在µ²äöüßÉõ之类的问题将被删除,但此代码不会)也假设有没有公式

Sub test()
  With ActiveSheet.UsedRange
    .Value = Evaluate("TRIM(CLEAN(" & .Address & "))")
  End With
End Sub

或直接在立即窗口中运行这个单行:

ActiveSheet.UsedRange.Value = Evaluate("TRIM(CLEAN(" & ActiveSheet.UsedRange.Address & "))")

答案 1 :(得分:2)

如果是我,我会在每次找到无效的char时对原始字符串使用replace命令,将该无效的char更改为null。然后用修改后的字符串替换原始单元格值。像这样......

一种可能的方式(测试)

<div style="z-index: 3; position: absolute; top: 50px; left: 50%; transform: translateX(-50%);">
  <div style="display: flex; background-color: rgb(173, 216, 230);">
    <div style="width: 200px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: left; white-space: nowrap;">tesdfxt stuff</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">more</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">29</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">Standard</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
  </div>
  <div style="display: flex; background-color: rgb(173, 216, 100);">
    <div style="width: 200px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: left; white-space: nowrap;">tesdfxt stuff</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">more</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">29</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">Standard</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
  </div>
  <div style="display: flex; background-color: rgb(173, 216, 230);">
    <div style="width: 200px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: left; white-space: nowrap;">sume stufff</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">more</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">29</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">Standard</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
  </div>
</div>


<div style="z-index: 3; position: absolute; top: 150px; left: 50%; transform: translateX(-50%);">
  <div style="display: flex; background-color: rgb(173, 216, 230);">
    <div style="width: 200px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: left; white-space: nowrap;">tesdfxt stuff</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">more</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">29</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">Standard</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
  </div>
  <div style="display: flex; background-color: rgb(173, 216, 100);">
    <div style="width: 200px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: left; white-space: nowrap;">tesdfxt stuff</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">more</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">29</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">Standard</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
  </div>
  <div style="display: flex; background-color: rgb(173, 216, 230);">
    <div style="width: 200px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: left; white-space: nowrap;">so much stuff tipsois yh stuff</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">more</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">29</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">Standard</div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 100px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;"></div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
    <div style="width: 75px; font-family: monospace; background-color: transparent; border: 1px solid black; overflow: hidden; text-align: center; white-space: nowrap;">0</div>
  </div>
</div>

答案 2 :(得分:2)

您还可以使用正则表达式,从而避免需要检查循环中的每个字符。 (虽然正则表达式引擎必须这样做)。

下面解释的正则表达式模式包含您的字符列表,使用的字符类表示匹配未列出的所有内容。

如果速度成为问题,您可以使用vba数组加快速度。

Option Explicit
Sub ReplaceNonStdChars()
    Const sPat As String = "[^\x20-\x7E\x99\xAE]"
    Dim RE As Object
    Dim R As Range, C As Range

Set R = Worksheets("features").UsedRange.SpecialCells(xlCellTypeConstants, xlTextValues)

Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = True
    .Pattern = sPat
    For Each C In R
        C.Value = .Replace(C.Text, "")
    Next C
End With
End Sub

正则表达式

的说明

[^ \ x20- \ x7E \ X99 \ XAE]

[^\x20-\x7E\x99\xAE]

使用RegexBuddy

创建

答案 3 :(得分:1)

我今天必须这样做,字面意思。下面的脚本对我来说非常好。

Sub Clean_and_Trim_Cells()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim s As String
    For Each c In ActiveSheet.UsedRange
        s = c.Value
        If Trim(Application.Clean(s)) <> s Then
            s = Trim(Application.Clean(s))
            c.Value = s
        End If
    Next
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub