简体   繁体   中英

Delete All Cells if not A-Z

I am working through 6 columns of data (AF) rows 2-4379 and a large number of the cells appear as "Blanks" in the filter column but are not true blanks as they seem to contain spaces in them. I was hoping to find some vba examples for finding all cells within a range that contained ASCII values between 65-90 and 97-122 and if those values were not contained within a cell, then to clear it completely.

Is this possible? I have tried a sub that checked for "IsText" but kept getting a "sub or function not defined" error message relating to the IsText line.

This is what I have tried so far:

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

This should remove most spaces from the active sheet:

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

.

As a note:

Your initial code had the error because you didn't include it in a Sub()

You can fix it by using a structure similar to this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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