【发布时间】:2017-08-10 11:05:20
【问题描述】:
我正在尝试通过删除任何非标准字符来清理 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 不够熟悉,无法真正知道如何去做。非常感谢任何见解!
【问题讨论】: