Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / November 2007

Tip: Looking for answers? Try searching our database.

Delete Columns if value equals zero

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Freddy - 28 Nov 2007 00:47 GMT
Hi guys,

I am using the following code to delete rows if the values in column Z = 0

Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
   CalcMode = .Calculation
   .Calculation = xlCalculationManual
   .ScreenUpdating = False
End With
With ActiveSheet
   .Select
   ViewMode = ActiveWindow.View
   ActiveWindow.View = xlNormalView
   .DisplayPageBreaks = False
   Firstrow = 5
   Lastrow = 466
   For Lrow = Lastrow To Firstrow Step -1
       With .Cells(Lrow, "Z")
           If Not IsError(.Value) Then
               If .Value = 0 Then .EntireRow.Delete
           End If
       End With
   Next Lrow
End With

I now have a table of values from columns B to W

I was wondering if it is possible to modify the above code or even get help
with a new code that will do the following:

Is there a way to create a row at the end of my table which has the sums of
each column from B to W

Find the last row of values (which has the totals in it)

If any of the values in this row equal zero, then to delete that column

The number of rows will always vary so cannot be set

As always, your help is greatly appreciated
Nigel - 28 Nov 2007 07:16 GMT
You can detect the last row of data using.... (uses column A to look for
last row, can be changed to any column or you can use index number e.g. for
"A" use 1 etc.)

Lastrow = Cells(Rows.Count,"A").End(xlup).Row

So to put your totals in the next available row, Lastrow + 1

To add the formula use

Cells(Lastrow+1,2).Formula = "=Sum(B5:B" & Lastrow & ")"

You can then copy across the formula into all other columns (use the macro
recorder to get the code)

Finally text each column total for zero

For myCol = 23 to 2 step -1
  If Cells(Lastrow+1,myCol).Value = 0 then
       Columns(myCol).Delete
 End If
Next myCol

Signature

Regards,
Nigel
nigelnospam@9sw.co.uk

> Hi guys,
>
[quoted text clipped - 43 lines]
>
> As always, your help is greatly appreciated
Freddy - 28 Nov 2007 11:41 GMT
Nigel,

This works perfectly

You really helped me out on this one.

> You can detect the last row of data using.... (uses column A to look for
> last row, can be changed to any column or you can use index number e.g. for
[quoted text clipped - 66 lines]
> >
> > As always, your help is greatly appreciated
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.