PC Review


Reply
Thread Tools Rate Thread

Delete Columns if value equals zero

 
 
Freddy
Guest
Posts: n/a
 
      28th Nov 2007
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
 
Reply With Quote
 
 
 
 
Nigel
Guest
Posts: n/a
 
      28th Nov 2007
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

--

Regards,
Nigel
(E-Mail Removed)



"Freddy" <(E-Mail Removed)> wrote in message
news:E2DD1BC3-EE11-404D-86D7-(E-Mail Removed)...
> 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


 
Reply With Quote
 
Freddy
Guest
Posts: n/a
 
      28th Nov 2007
Nigel,

This works perfectly

You really helped me out on this one.

"Nigel" wrote:

> 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
>
> --
>
> Regards,
> Nigel
> (E-Mail Removed)
>
>
>
> "Freddy" <(E-Mail Removed)> wrote in message
> news:E2DD1BC3-EE11-404D-86D7-(E-Mail Removed)...
> > 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

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overriding virtual function Equals affecting static Equals... Raj Microsoft C# .NET 7 17th Mar 2010 04:56 AM
How do I automatically delete a row, if one cell equals zero? Koz Microsoft Excel Worksheet Functions 6 13th Aug 2008 08:42 PM
Delete Row if Value in Column L equals zero Aaron Microsoft Excel Programming 3 28th Nov 2007 04:23 PM
delete every row where value equals user input Mitchell_Collen via OfficeKB.com Microsoft Excel Programming 2 18th Oct 2007 07:10 PM
Only show rows where T1_F1 equals T2_F1 and T1_F2 equals T2_F2 and =?Utf-8?B?TmljaG9sYXMgS29ybWFuaWs=?= Microsoft Access Queries 3 18th Nov 2005 06:33 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:02 PM.