Closed Thread
Results 1 to 36 of 36

Delete cells with a specific value

  1. #1
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Delete cells with a specific value

    Hi,

    I am not a new user of Excel but i think this would require use of a macro which i am new to. Probably this wud be very straightforward for you, but i wanted to delete cells containing values "0" and "1" in all the columns. Could you please help me with that.

    Thanks,
    Aman
    Last edited by agupta; 05-23-2011 at 03:48 PM.

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Delete cells with a specific value

    Do you want to clear them or delete them? If you delete them, which way do you want your spreadsheet to shift (to the left or up)?
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Thanks for the reply chemist! I want to delete the cells and shift the the spreadsheet up.Thanks for your help.

  4. #4
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Delete cells with a specific value

    Try this macro
    Please Login or Register  to view this content.
    Does that work for you?

  5. #5
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    I tried the code, but strangely it deleted some of them but not all!

  6. #6
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    i have all the values in the cells with two decimal places like 0.45, 0.69, 0.00,1.00. Wud that make a difference. Is that why it deletes some cells and not others. I also notice that it has deleted some of the cells other then 0.00 and 1.00.

  7. #7
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    I would appreciate if somebody could please help me on this.
    Thanks.

  8. #8
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Delete cells with a specific value

    Hi,

    See if this works for you:
    Please Login or Register  to view this content.
    Hope that helps,

    Colin

    RAD Excel Blog

  9. #9
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Thanks for the code Collin. It's the same problem again, the code deleted many of the values but not all. It hope it's not because of the decimal places?

  10. #10
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Delete cells with a specific value

    Hi - the code I posted will delete values which are exactly 0 or 1. It doesn't matter if the 1 is formatted as 1.00.

    Please post a sample workbook.
    Last edited by Colin Legg; 05-23-2011 at 02:11 PM.

  11. #11
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Thanks Collin. I have attached the workbook. Can you please run the macro on the first sheet (SemiT) and see why is it not deleting all the 0's and 1's?Thanks for your help.
    Attached Files Attached Files

  12. #12
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Delete cells with a specific value

    If you click on D8 (for example) you'll see in the formula bar, the actual value is 0.99999988079071.

  13. #13
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Ahhh.....there u go! I should have thought of that! Thanks.

  14. #14
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Delete cells with a specific value

    Hi agupta

    It leaves some cells because they are not exactly equal to 0 or 1.

    For example, if you look at cell D8, because it is formatted to 2 decimal places it is displayed as 1.00, but if you look in the formula bar you can see that the real value is 0.99999988079071.

  15. #15
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Delete cells with a specific value

    Quote Originally Posted by ChemistB View Post
    If you click on D8 (for example) you'll see in the formula bar, the actual value is 0.99999988079071.
    LOL... how funny that we both picked the same cell to demonstrate the point.

  16. #16
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    My bad. should have figured that out. Anyways, thanks Colin and ChemistB for your help.

  17. #17
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Delete cells with a specific value

    try multiplying the whole lot by 100000000000 then divide by 100000000000 that should change

    0.99999988079071 to 1
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  18. #18
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    @Martin: Actually, i want to keep 0.99 as 0.99 without rounding it off to 1.I want macro to only delete cells with exact 0's and 1's. Do you know how can i make excel to NOT round off the values to 1?

  19. #19
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Delete cells with a specific value

    It's not actually rounding them. It's just showing them to 2 decimal places. If you format the cells to General, you'll see the whole thing. You'll also need to adjust your cell width.

  20. #20
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    @Chemist: I know, when i change the cell width it shows me the entire thing, but can i make it show me only 2 decimal places but without rounding off?

  21. #21
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Delete cells with a specific value

    Not without a formula or code. =TRUNC(A1,2) truncates a value rather than rounding it. Sorry.

  22. #22
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Thanks chemist for the solution. And sorry to bug you again and again but this would be my last question. Can you tell me if i can apply this formula on the values in the same cell as the values itself, meaning, can i get the result in the same cell on which i apply the formula without creating a new column to get the results?
    Thanks a lot!

  23. #23
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    I keep on running into trouble on this. In the attached sheet, i first truncated the values to 2 decimal places and then ran the macro to delete cells with 0's and 1's. It still shows me some 0's and 1's even after running the code! Can you please have a look at it.
    Thanks.
    Attached Files Attached Files

  24. #24
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Delete cells with a specific value

    hi Agupta,

    I can't see any code in the file you've uploaded. Can you please reattach a file which contains your latest version of the code?

    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  25. #25
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Thanks broro183. Sure, please find the file attached containing the code. Thanks.
    Attached Files Attached Files

  26. #26
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Delete cells with a specific value

    Hi,

    My suggestion is that you try running the code I posted in #8 instead.

  27. #27
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Delete cells with a specific value

    hi,

    Blimey!
    Post #8 is waaaaaayyyyy back in the thread. I think it's the way to go

    I can't confidently say why the current code is missing some 0's & 1's but I imagine that the act of deleting the cells one at a time during the process changes what excel/vba interprets as the TotalRng. This would be consistent with having a cell linked to another cell using a formulae & then deleting the precedent cell, ie the result of the dependent cell will then show a #REF! error. Colin's code looks good to me because it avoids this issue of deleting cells during the loop. Also, the act of deleting one unioned range should be faster, in this case, than the multiple individual deletions

    Rob

  28. #28
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Thumbs up Re: Delete cells with a specific value

    Thankyou very much everyone. Colin's code worked!

  29. #29
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Hi Colin,

    I again needed to use your code mentioned in # 8 for removing 0's and 1's, but this time i want to keep the cells blank instead of shifting the cells up. Can you please modify the code for that?

    Thankyou very much,
    Aman

  30. #30
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Delete cells with a specific value

    Sure, just change this line:
    Please Login or Register  to view this content.
    To this:
    Please Login or Register  to view this content.
    Of course, you might want to change the variable name throughout the code to something more appropriate - such as rngToClear.

  31. #31
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Thanks a lot Colin!

  32. #32
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Hi Colin,

    I needed a little more modification in the code. Can you please modify the code to remove/delete the entire row for a cell containing 0's and 1's instead of just deleting that particular cell?

    Thanks,
    Aman

  33. #33
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Delete cells with a specific value

    Sure, this line of code:
    Please Login or Register  to view this content.
    Becomes:
    Please Login or Register  to view this content.

  34. #34
    Registered User
    Join Date
    05-19-2011
    Location
    Chicago
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Delete cells with a specific value

    Awesomeness! Thanks.

  35. #35
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Delete cells with a specific value

    Acutally, not quite. It will error if there are non-contiguous cells containing 1 or 0 on the same row. Amend that single line to this instead:
    Please Login or Register  to view this content.

  36. #36
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Delete cells with a specific value

    Agupta,
    when you have a new question, please start a new thread. If you feel it is necessary, add a link to the original one.

    Thread closed

Closed Thread

Thread Information

Users Browsing this Thread

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

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