如果不是A-Z,则删除所有单元格

时间:2015-06-25 15:23:20

标签: excel vba excel-vba ascii

我正在处理6列数据(A-F)行2-4379,并且大量单元格显示为"空白"在过滤器列中但不是 true 空白,因为它们似乎包含空格。我希望找到一些vba示例,用于查找范围内包含介于65-90和97-122之间的ASCII值的所有单元格,如果这些值未包含在单元格中,则完全清除它。

这可能吗?我试过一个检查" IsText"但仍然得到一个" sub或函数未定义"与IsText行相关的错误消息。

这是我到目前为止所尝试的:

Dim c As Range
Dim rng As Range

Set rng = Range("A2:F4379")

For Each c in rng

If Not IsText(c.Value) Then
c.ClearContents
End If

Next c

1 个答案:

答案 0 :(得分:1)

这应该从活动工作表中删除大多数空格:

Option Explicit

Public Sub trimWhiteSpaces()

    With ActiveSheet.UsedRange

        .Replace What:=" ", Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:="  ", Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:="   ", Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:="    ", Replacement:=vbNullString, LookAt:=xlWhole

        .Replace What:=vbTab, Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:=vbCrLf, Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:=vbCr, Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:=vbLf, Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:=vbNewLine, Replacement:=vbNullString, LookAt:=xlWhole

        .Replace What:=vbNullChar, Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:=vbBack, Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:=vbFormFeed, Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:=vbVerticalTab, Replacement:=vbNullString, LookAt:=xlWhole
        .Replace What:=vbObjectError, Replacement:=vbNullString, LookAt:=xlWhole

    End With

End Sub

作为说明:

您的初始代码出现错误,因为您没有将其包含在Sub()

您可以使用与此类似的结构来修复它:

Option Explicit

Public Sub testSub()
    Dim c As Range
    Dim rng As Range

    Set rng = Range("A2:F4379")

    For Each c In rng
        If Not IsText(c.Value) Then
            c.ClearContents
        End If
    Next
End Sub