Solved

Delete all zeros in column A using VBA

Posted on 2011-09-04
7
440 Views
Last Modified: 2012-05-12
Dear Experts:

I would like to achieve the following using VBA:

Delete all zeros (0) in column A of the active worksheet regardless of their formatting, i.e. zeros formatted as text or number.

Help is much appreciated.  Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
7 Comments
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 500 total points
ID: 36480417
Hi,

Try

Sub kTest()

    With ActiveSheet.Columns(1)
        .Replace "0", "", 1
        '//if you want to delete the rows uncomment the following 3 line

'        On Error Resume Next
'        .SpecialCells(4).EntireRow.Delete
'        On Error GoTo 0
    End With

End Sub

Kris
0
 
LVL 5

Expert Comment

by:slycoder
ID: 36480748
In Excel 2007 and above - you can Hide all zeros in the worksheet with - File -  Options - Advanced - Display Options For This Worksheet -  and uncheck: Show a Zero in Cells that have a Zero Value.
0
 

Author Comment

by:AndreasHermle
ID: 36480902
Hi Kris,

thank you very much for your swift help. I am afraid to tell you that nothing happens when running your code. My requirements may have been a little bit to vague.

The following code which I managed to set up myself does what I want. It is a little bit slow though. And I wonder that all Zeros are deleted even if they are formatted as numbers.

Regards, Andreas


Sub kTestfinal()

Dim cel As Range

For Each cel In ActiveSheet.Columns(1).Cells
If cel.Value = "0" Then
cel.ClearContents
End If
Next cel

Open in new window

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:AndreasHermle
ID: 36480904
Hi slycoder:

thank you very much for your feedback. I am aware of this feature, but it is regrettably of no use for my requirements.

Regards, Andreas
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36481087
Andreas,

Do you mean "clear" the cell, or delete the cell?

If delete, do you mean delete just that cell, or the whole row?

If delete just that cell, do you want to shiftthe cells below it up, or the cells to the right leftward?

Patrick
0
 

Author Comment

by:AndreasHermle
ID: 36481196
Hi Patrick:

I mean 'clear' the cell

Thank you, Regards, Andreas
0
 
LVL 18

Expert Comment

by:krishnakrkc
ID: 36482192
Hi

It works fine for me regardless of their formatting.

Are you able to do this manually ?

Hit Ctrl + H
Find what: 0
Leave 'Replace with' box empty
Replace All
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question