Results 1 to 7 of 7

Thread: [RESOLVED] Fastest way to clear cells based on cel length

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Resolved [RESOLVED] Fastest way to clear cells based on cel length

    I have a huge amount of data (8000 rows x 100 columns). Most cells have a length of 3, many others are blank and some have lengths of 1 or 2. I need to clear cells if the cell length isn't 3. I know of course that I can do this
    Code:
    Dim cel As Range
    For Each cel In MyRange
        If Len(cel) = 2 Or Len(cel) = 1 Then
            cel = ""
        End If
    Next
    but I'm looking for a faster way. Any ideas?

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,264

    Re: Fastest way to clear cells based on cel length

    OUCH!
    The only thing coming to mind would be to set a custom Autofilter "equals not ???" (question-marks are wildcards for single characters).
    But i'm not sure if Autofilter "changes" the Range it's applied on or if it returns the changed range,
    The idea would be to apply "ClearContents" to that range.

    You would have to step columnwise through your sheet and apply above idea, so basically a loop 1 to 100.

    kind of like (adjust columnwise or sheetwise)
    Code:
        ActiveSheet.Range("$A$2:$A$8000").AutoFilter Field:=1, Criteria1:="<>???", Operator:=xlAnd
        ActiveSheet.AutoFilter.Range.ClearContents
    Last edited by Zvoni; Jun 19th, 2023 at 09:30 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,742

    Re: Fastest way to clear cells based on cel length

    Minuscule improvement
    Code:
     If Len(cel) < 3 Then

  5. #5

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Fastest way to clear cells based on cel length

    Quote Originally Posted by Zvoni View Post
    OUCH!
    The only thing coming to mind would be to set a custom Autofilter "equals not ???" (question-marks are wildcards for single characters).
    But i'm not sure if Autofilter "changes" the Range it's applied on or if it returns the changed range,
    The idea would be to apply "ClearContents" to that range.

    You would have to step columnwise through your sheet and apply above idea, so basically a loop 1 to 100.

    kind of like (adjust columnwise or sheetwise)
    Code:
        ActiveSheet.Range("$A$2:$A$8000").AutoFilter Field:=1, Criteria1:="<>???", Operator:=xlAnd
        ActiveSheet.AutoFilter.Range.ClearContents
    Thanks.

  6. #6
    Addicted Member
    Join Date
    May 2021
    Posts
    131

    Re: [RESOLVED] Fastest way to clear cells based on cel length

    I know this is already resolved, but another option is to convert the entirety of the range into a single string. Using the Names method (see below), this will return a string like:

    Attachment 187992

    ={"Are","Kara","Bokutachi","Wa";"Nani","Ka","Wo","Shinjite";"Koreta","Ka","Na","!"}

    Code:
    Sub ConvertToString()
    
        Dim TargetName As Name
        Set TargetName = ThisWorkbook.Names.Add("JPOP", ThisWorkbook.Sheets("Sheet1").Range("A1:D3").Value)
        Debug.Print Names("JPOP")
        
        ' Output: ={"Are","Kara","Bokutachi";"Nani","Ka","Wo";"Koreta","Ka","Na"}
        
    End Sub
    Once it's in a string (and in memory), it should be a lot quicker than having to deal with the Range object. You could conceivably use the Regex replace method to find anything in the string (between quotation marks) of fewer than 3 characters in length and replace it with vbNullString.

    Haven't tested the above regex step, but I assume it'll work.

  7. #7
    Lively Member
    Join Date
    Jul 2017
    Posts
    117

    Re: [RESOLVED] Fastest way to clear cells based on cel length

    Code:
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
            .DisplayStatusBar = False
            .EnableEvents = False
        End With
        
        Dim vSheet As Variant
        vSheet = Sheets("Sheet1").Range("A1:BZ8000")
        
        Dim i As Long, j As Long
        Dim lLength As Long
        Dim lJUB As Long
        lJUB = UBound(vSheet, 2)
        
        For i = 1 To UBound(vSheet, 1)
            For j = 1 To lJUB
                lLength = Len(vSheet(i, j))
                If lLength = 1 Then
                    vSheet(i, j) = vbNullString
                ElseIf lLength = 2 Then
                    vSheet(i, j) = vbNullString
                End If
            Next j
        Next i
        
        Sheets("Sheet1").Range("A1:BZ8000") = vSheet
        With Application
            .DisplayStatusBar = True
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width