Consulting

Results 1 to 12 of 12

Thread: Clear cell data that does not contain specific value

  1. #1

    Clear cell data that does not contain specific value

    I have a code that clears data in a cell not containing the word (Worknotes). However, for 240 rows of data the code ran for an hour. Please assist on how to reduce the time to process as I am looking at using this for a much larger set of data.

    Sub ClearCells()


    Dim rng As Range
    Dim cell As Range
    Dim ContainWord As String


    Set rng = Range("N2:AJT240")


    ContainWord = "(Work notes)"


    For Each cell In rng.Cells
    If cell.Find(ContainWord) Is Nothing Then cell.Clear
    Next cell


    End Sub

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Sub test()
        Dim rng As Range
        Dim cell As Range
        Dim ContainWord As String
        Dim u As Range
    
        Set rng = Range("N2:AJT240")
        ContainWord = "(Work notes)"
         
        For Each cell In rng.SpecialCells(xlCellTypeConstants)
            If InStr(cell.Value, ContainWord) = 0 Then
                If u Is Nothing Then
                    Set u = cell
                Else
                    Set u = Union(u, cell)
                End If
            End If
        Next cell
        
        If Not u Is Nothing Then u.Clear
    
    End Sub

  3. #3
    Thanks for the response. However, this script also is taking a lot of time to run and excel stops responding.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Range("N2:AJT240"): That is a very large range = 238 rows time 256 to the 6 power of 256 columns. Or something like that. Whatever, It's huuuuge.

    Is there no way to shrink that? Example of only certain columns:
    With Rows(2:240)
       With Columns("n,z, aj, abn, abz,etc
    Query? Do more cells contain that string or do more NOT contain that string?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
      sn= Range("N2:AJT240")
     
      for j=1 to ubound(sn)
        for jj=1 to ubound(sn,2)
          if instr(sn(j,jj),"Work notes")=0 then sn(j,jj)=""
        next
      next
    
      Range("N2:AJT240")=sn
    End Sub

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Betchas snb's code works faster than you can count to 3
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    @Sam

    ,875 sec on my system.

  8. #8
    I get a runtime error '13', Type mismatch. This part of the code is highlighted:

    if instr(sn(j,jj),"Work notes")=0 then

  9. #9
    There is a lot of data that is not required from N2:AJT240. The data exists due to a text to columns for which we need to get a particular set of data. This data does not sit under a particular column. It is spread randomly.

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    So what is the value of sn(j,jj) in this case ?

    remove all errors in the range before running the macro.

  11. #11
    I'm sorry I do not follow. very new to VBA

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The data exists due to a text to columns
    What is the format of the raw data?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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