繁体   English   中英

删除所有单元格(如果不是AZ)

[英]Delete All Cells if not A-Z

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

这可能吗? 我尝试了一个子程序,该子程序检查了“ IsText”,但始终收到与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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM