Power Query Course in Spanish
Thanks Thanks:  0
Likes Likes:  0
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: clear cells using VBA

  1. #1
    New Member
    Join Date
    Apr 2002
    Posts
    16
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I need to clear all cells in a column that have a particular value. The value is sometimes a constant like "0" (zero) and sometimes is a value based on a formula. Is there a VBA code that will allow me to select those cells in column matching a particular value and clear them?

    Cheers

  2. #2
    Board Regular
    Join Date
    Feb 2002
    Posts
    3,184
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    why use vba find and replace


    Free Excel based Web Toolbar available here.

    Jack in the UK
    J & R Excel Solutions
    "making Excel work for you"

  3. #3
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,700
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    This should work on formulae results or values:

    Code:
    Sub deltime()
    For Each Cell In [a:a]
    If Cell.Value = "0" Then Cell.ClearContents 'put any value you want here
    Next Cell
    End Sub
    Change [a:a] to the appropriate column. Hope this helps.

    _________________
    Cheers, NateO
    "Me no are no nice guy."

    [ This Message was edited by: NateO on 2002-04-25 15:49 ]

  4. #4
    New Member
    Join Date
    Apr 2002
    Posts
    16
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks guys, that worked fine

  5. #5
    Board Regular
    Join Date
    Aug 2009
    Posts
    134
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: clear cells using VBA

    This looks really helpful, when i try is though i get
    Run Time Error 13:

    "Type Mismatch"

    and the below is highlighted

    If Cell.Value = "00/01/1900" Then

    I have tried changing the month and day around and also tried changing it to "ben" and the same error occurs.

    Secondly, will this work on a VLOOKUP result, which 00/01/1900 is

    Thanks

    Ben

    PS. I've tried this on some other columns and managed to get it mostly working, it seems the VLOOKUP result is scuppering things, any suggestions?
    Last edited by Bench; Sep 29th, 2009 at 08:06 AM. Reason: Additional info

  6. #6
    Board Regular
    Join Date
    Sep 2005
    Posts
    135
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: clear cells using VBA

    that code won't work for a vlookup result. to do that you need to copy the vlookup result and paste it as a value. so if column A is your vlookup column, I'd copy and paste column A onto itself as values and then do a find/replace for specific values.

  7. #7
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    31,916
    Post Thanks / Like
    Mentioned
    17 Post(s)
    Tagged
    3 Thread(s)

    Default Re: clear cells using VBA

    Do you have any error values in your data? You will get a Type Mismatch error if you try and compare error values like that.

  8. #8
    Board Regular
    Join Date
    Aug 2009
    Posts
    134
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: clear cells using VBA

    Thanks i've tried that but still no luck, even changed from date to general, i think it may be as Rorya says, "00/01/1900" is the result i'm trying to lose, this i guess is an error as it is saying that the data in the other spreadsheet is blank.

    Is there a way around this, a way to blank out the errors? and not simply changing the font to white

    I'm wondering also if it may be the way date is formatted within the VBA, should it be done dd/mm/yyyy or is there another format required?
    Last edited by Bench; Sep 29th, 2009 at 11:15 AM. Reason: additional info

  9. #9
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    31,916
    Post Thanks / Like
    Mentioned
    17 Post(s)
    Tagged
    3 Thread(s)

    Default Re: clear cells using VBA

    00/01/1900 is not an error. If the VLOOKUP were returning an error, you would see #N/A in the cell. You could try using:
    Code:
    If Cell.Value2 = 0 then

  10. #10
    Board Regular
    Join Date
    Sep 2005
    Posts
    135
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: clear cells using VBA

    You could add IF logic to your VLOOKUP to evaluate if it returns #N/A or #VALUE! using the ISERROR function. It will require you to have the VLOOKUP in the cell formula more than once, so on large spreadsheets it might be a concern since the VLOOKUP is a volatile function.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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