I need a Excel macro to delete zeros only and no zeros containing more numbers.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ManuelValdez
    New Member
    • Jan 2008
    • 5

    I need a Excel macro to delete zeros only and no zeros containing more numbers.

    Hello everybody!

    I need your valuable help to get an Excel macro to delete the single zeros only and no the zeros containing numbers like 360, 90, etc., because if I chose the search and replace in the Edit option, then all the zeros containing the numbers 360, 90, 502, etc. will be deleted.

    Some nice person helped me with the following code to delete the zeros:
    Selection.Repla ce What:="0", Replacement:="" , but unfortunately this code delete all the zeros including the zeros containing more numbers.

    Thank you very mucho for your help.
  • Wagz
    New Member
    • Feb 2008
    • 11

    #2
    If you want to use a macro to delete the zeros then you can use the following code
    Code:
    For Each cell In Selection
        If cell = "0" Then
            cell.Delete
        End If
    Next
    This will go through all of the selected cells and delete any that are zero.

    However you can also use search and replace edit option that you described, just make sure to check the "match entire cells contents".

    Wagz

    Comment

    • kadghar
      Recognized Expert Top Contributor
      • Apr 2007
      • 1302

      #3
      Originally posted by Wagz
      If you want to use a macro to delete the zeros then you can use the following code
      Code:
      For Each cell In Selection
          If cell = "0" Then
              cell.Delete
          End If
      Next
      This will go through all of the selected cells and delete any that are zero.

      However you can also use search and replace edit option that you described, just make sure to check the "match entire cells contents".

      Wagz
      That's right, Excel has the option to mach entire cells content, The code wagz wrote will help, but will delete the cell. If what you want is to delete the zero, but keep the cell empty, instead of

      cell.delete

      use
      cell.value = ""

      HTH

      Comment

      • ManuelValdez
        New Member
        • Jan 2008
        • 5

        #4
        Hello guys!

        I just want to thank you very much for your help!

        I am very grateful with you!

        I work with statistics most of the time and when I use the function to calculate the average the results vary if the cells have zeros and, of course, that affects the results.

        So, the code is doing what I really need!

        Have a very nice day!

        God bless you!

        Comment

        Working...