Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default How can I update macro to delete zero amount columns?

I have a spreadsheet that contains columns with text (such as name, job
title, hire date, etc.) and several columns with amounts. I want to delete
the "amount" columns that contain all zeros. I used the macro below and it
removed all the amount columns that contained just zeros but it also removed
the columns that contained the name, title, etc.

How can I change the macro below to remove only the zero amount columns and
leave the other text columns? Thanks for the assistance.

Sub RemoveColumns()
Dim nLastColumn As Long
Set r = ActiveSheet.UsedRange
nLastColumn = r.Columns.Count + r.Column - 1
For i = nLastColumn To 1 Step -1
If Application.WorksheetFunction.Sum(Columns(i)) = 0 Then
Columns(i).Delete
End If
Next
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default How can I update macro to delete zero amount columns?

Hi Nora:

Try this modification:

Sub RemoveColumns()
Dim nLastColumn As Long
Set r = ActiveSheet.UsedRange
nLastColumn = r.Columns.Count + r.Column - 1
For i = nLastColumn To 1 Step -1
i1 = Application.WorksheetFunction.Sum(Columns(i))
i2 = Application.WorksheetFunction.Count(Columns(i))
i3 = Application.WorksheetFunction.CountA(Columns(i))
If i1 = 0 And i2 = i3 Then
Columns(i).Delete
End If
Next
End Sub


You can use a similar technique to test for the existence of text cells in
worksheet formulas.
--
Gary''s Student - gsnu201003


"Nora_GG" wrote:

I have a spreadsheet that contains columns with text (such as name, job
title, hire date, etc.) and several columns with amounts. I want to delete
the "amount" columns that contain all zeros. I used the macro below and it
removed all the amount columns that contained just zeros but it also removed
the columns that contained the name, title, etc.

How can I change the macro below to remove only the zero amount columns and
leave the other text columns? Thanks for the assistance.

Sub RemoveColumns()
Dim nLastColumn As Long
Set r = ActiveSheet.UsedRange
nLastColumn = r.Columns.Count + r.Column - 1
For i = nLastColumn To 1 Step -1
If Application.WorksheetFunction.Sum(Columns(i)) = 0 Then
Columns(i).Delete
End If
Next
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default How can I update macro to delete zero amount columns?

Thanks again Gary's Student! I additional changes to the changes you
suggested and was able to achieve deleting all amount columns totaling zero
but leaving the columns with text data and dates alone. Really appreciate the
assistance!

Sub RemoveColumns()
Dim nLastColumn As Long
Set r = ActiveSheet.UsedRange
nLastColumn = r.Columns.Count + r.Column - 1
For i = nLastColumn To 1 Step -1
i1 = Application.WorksheetFunction.Sum(Columns(i))
i2 = Application.WorksheetFunction.Count(Columns(i))
If i1 = 0 And i2 < 0 Then
Columns(i).Delete
End If
Next
End Sub



"Gary''s Student" wrote:

Hi Nora:

Try this modification:

Sub RemoveColumns()
Dim nLastColumn As Long
Set r = ActiveSheet.UsedRange
nLastColumn = r.Columns.Count + r.Column - 1
For i = nLastColumn To 1 Step -1
i1 = Application.WorksheetFunction.Sum(Columns(i))
i2 = Application.WorksheetFunction.Count(Columns(i))
i3 = Application.WorksheetFunction.CountA(Columns(i))
If i1 = 0 And i2 = i3 Then
Columns(i).Delete
End If
Next
End Sub


You can use a similar technique to test for the existence of text cells in
worksheet formulas.
--
Gary''s Student - gsnu201003


"Nora_GG" wrote:

I have a spreadsheet that contains columns with text (such as name, job
title, hire date, etc.) and several columns with amounts. I want to delete
the "amount" columns that contain all zeros. I used the macro below and it
removed all the amount columns that contained just zeros but it also removed
the columns that contained the name, title, etc.

How can I change the macro below to remove only the zero amount columns and
leave the other text columns? Thanks for the assistance.

Sub RemoveColumns()
Dim nLastColumn As Long
Set r = ActiveSheet.UsedRange
nLastColumn = r.Columns.Count + r.Column - 1
For i = nLastColumn To 1 Step -1
If Application.WorksheetFunction.Sum(Columns(i)) = 0 Then
Columns(i).Delete
End If
Next
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I delete a space at the end of an amount or text Maureen Excel Worksheet Functions 5 June 2nd 10 12:06 PM
Auto Delete Columns in a Macro that are Blank dkeglor Excel Discussion (Misc queries) 1 October 31st 07 01:28 AM
How can I delete a macro when the Delete button is not active? FCR Excel Worksheet Functions 0 March 9th 06 09:43 AM
How do i delete a macro in Excel 2003 when delete isn't highlight Abel Excel Discussion (Misc queries) 2 September 13th 05 04:09 AM
Sum of columns until you a certain amount Andman Excel Discussion (Misc queries) 3 March 26th 05 02:27 PM


All times are GMT +1. The time now is 01:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"