?
Solved

Delete all zeros in column A using VBA

Posted on 2011-09-04
7
Medium Priority
?
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 93

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

770 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