+ Reply to Thread
Results 1 to 5 of 5

Macro - find cells row by row, clear contents if certain value in cells

Hybrid View

astrbac Macro - find cells row by... 12-17-2014, 11:30 AM
StephenR Re: Macro - find cells row by... 12-17-2014, 11:46 AM
Mumps1 Re: Macro - find cells row by... 12-17-2014, 11:47 AM
davesexcel Re: Macro - find cells row by... 12-17-2014, 11:57 AM
astrbac Re: Macro - find cells row by... 12-18-2014, 05:34 AM
  1. #1
    Registered User
    Join Date
    05-08-2014
    Location
    croatia
    MS-Off Ver
    Excel 2010
    Posts
    18

    Question Macro - find cells row by row, clear contents if certain value in cells

    Hi all,

    I am new to the whole "programming" in Excel and have actually discovered a whole new universe of possibilities . What I need to do is this:

    1. Sheet 1, cells from A1 to, say, G100 are filled with various numbers (integers) from 1 to 200; some of them repeat more times, they are in no particular order
    2. I wish to find all the cells that have values of for example: 3 OR 56 OR 143
    3. finally, I wish to clear contents of these cells (not delete cells, just leave them empty)

    I normally use "ready made" command from within Excel to do this (Ctrl+H which brings up Find&Replace) but that way I need to do it number by number/string by string. It would be great if I could input "xx AND xx AND xx..." in the Find fied but that isn't possible as far as I know. Also, does anything change if cells aren't filled with integers but with strings such as: "768x90", "978x90", "300x600", .....?

    Simple explanations if possible please I'm also trying to learn as much as possible along the way, not just solve a one off problem.

    Many thanks,
    Alex

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macro - find cells row by row, clear contents if certain value in cells

    This should start you off
    Sub x()
      
    Dim rFind As Range, v, i As Long
     
    v = Array(3, 56, 143)
    
    With Range("A1:G100")
        For i = LBound(v) To UBound(v)
            Set rFind = .Find(What:=v(i), LookIn:=xlFormulas, LookAt:=xlWhole, _
                              MatchCase:=False, SearchFormat:=False)
            If Not rFind Is Nothing Then
                Do
                    rFind.ClearContents
                    Set rFind = .FindNext(rFind)
                Loop Until rFind Is Nothing
            End If
            Set rFind = Nothing
        Next i
    End With
         
    End Sub

  3. #3
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: Macro - find cells row by row, clear contents if certain value in cells

    Hi Alex. If the numbers you want to clear are going to change each time, you could be prompted for the numbers you want to clear but it would be one number at a time. If the numbers you want to clear are always the same ones, then they can be cleared all at once. If you want to clear the cells if they also contain strings, that is possible as well. Does one of the approaches work for you?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Macro - find cells row by row, clear contents if certain value in cells

    Try this.....

    Sub Find_Stuff()
        Dim rng As Range, c As Range
        Set rng = Range("A1:G100")
    
        For Each c In rng.Cells
            If c = 3 Or c = 56 Or c = 143 Then c = ""
        Next c
        
    End Sub

  5. #5
    Registered User
    Join Date
    05-08-2014
    Location
    croatia
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Macro - find cells row by row, clear contents if certain value in cells

    Thanks all! These both seem to work, now I'm going to investigate a bit and try to break these down.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to Clear contents in a range of cells when Date change.
    By aneshdas in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-08-2014, 01:53 AM
  2. [SOLVED] I need a macro to clear cells based on contents
    By emymeeky in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-28-2013, 08:54 AM
  3. Macro to Clear Contents of Cells when one cell = 0 %
    By BVT3030 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-09-2013, 02:55 AM
  4. Macro to clear only the contents of the cells which have a specific font colour
    By Stellakyp in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-05-2013, 11:01 AM
  5. [SOLVED] Macro to clear contents of unprotected cells AND drop down boxes
    By JB2010 in forum Excel General
    Replies: 3
    Last Post: 03-30-2006, 05:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1