Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default Remove columns with all zeros

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Remove columns with all zeros

Try this:

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

--
Gary''s Student - gsnu201003


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default Remove columns with all zeros

I would use COUNTIF() instead..

Sub DeleteColumnswithZeros()
Dim lngCol As Long, lngLastCol As Long

lngLastCol = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
For lngCol = lngLastCol To 1 Step -1
If WorksheetFunction.CountIf(Columns(lngCol), 0) + _
WorksheetFunction.CountBlank(Columns(lngCol)) = _
Rows.Count Then Columns(lngCol).Delete
Next
End Sub


--
Jacob (MVP - Excel)


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default Remove columns with all zeros

Thank you Gary's Student. I am pretty new to building macros. I created the
macro below in a separate workbook and then opened it in the workbook I
needed to apply the macro. Should I have created the macro in the workbook
that needed the columns removed? Also, did I need to include a range in the
macro below or was it ok to copy as is? Appreciate the assistance.

"Gary''s Student" wrote:

Try this:

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

--
Gary''s Student - gsnu201003


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default Remove columns with all zeros

Thank you Jacob. I am pretty new to building macros. I created the macro
below in a separate workbook and then opened it in the workbook I needed to
apply the macro. Should I have created the macro in the workbook that needed
the columns removed? Also, did I need to include a range in the macro below
or was it ok to copy as is? Appreciate the assistance.

By the way, I created the macro below by selecting the Macro option under
Tools.

Thanks again.

"Jacob Skaria" wrote:

I would use COUNTIF() instead..

Sub DeleteColumnswithZeros()
Dim lngCol As Long, lngLastCol As Long

lngLastCol = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
For lngCol = lngLastCol To 1 Step -1
If WorksheetFunction.CountIf(Columns(lngCol), 0) + _
WorksheetFunction.CountBlank(Columns(lngCol)) = _
Rows.Count Then Columns(lngCol).Delete
Next
End Sub


--
Jacob (MVP - Excel)


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 22,906
Default Remove columns with all zeros

For a start you would be best off copying and pasting the macro as is to a
general module in the workbook to acted upon.

Otherwise you would have to qualify which workbook has the ActiveSheet.

UsedRange should take care of all columns and rows that Excel sees as being
in use on the active worksheet.


Gord Dibben MS Excel MVP

On Wed, 2 Jun 2010 15:28:24 -0700, Nora_GG
wrote:

Thank you Gary's Student. I am pretty new to building macros. I created the
macro below in a separate workbook and then opened it in the workbook I
needed to apply the macro. Should I have created the macro in the workbook
that needed the columns removed? Also, did I need to include a range in the
macro below or was it ok to copy as is? Appreciate the assistance.

"Gary''s Student" wrote:

Try this:

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

--
Gary''s Student - gsnu201003


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks


  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default Remove columns with all zeros

Hello. I set up teh macro and it removed teh columns with all zeros. It also
removed columns containing text data. How can I tweak the macro below so it
ignores columns that contain data such as names, job title, etc.

Thanks again for your asisstance.

"Gary''s Student" wrote:

Try this:

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

--
Gary''s Student - gsnu201003


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks

  #8   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default Remove columns with all zeros

If you are new to macros..

--Set the Security level to low/medium in (Tools|Macro|Security).
--From workbook launch VBE using short-key Alt+F11.
--From menu 'Insert' a module and paste the below code.
--Get back to Workbook.
--Run macro from Tools|Macro|Run <selected macro()


--I created the macro below in a separate workbook and then opened it in the
workbook I needed to apply the macro. Should I have created the macro in the
workbook that needed the columns removed?

No need; the workbook in which you need to delete the columns should be the
active workbook..

--Also, did I need to include a range in the macro below or was it ok to
copy as is? Appreciate the assistance.

No need; the macro identifies the last column with values and check for zero
cells upto that column.

--
Jacob (MVP - Excel)


"Nora_GG" wrote:

Thank you Jacob. I am pretty new to building macros. I created the macro
below in a separate workbook and then opened it in the workbook I needed to
apply the macro. Should I have created the macro in the workbook that needed
the columns removed? Also, did I need to include a range in the macro below
or was it ok to copy as is? Appreciate the assistance.

By the way, I created the macro below by selecting the Macro option under
Tools.

Thanks again.

"Jacob Skaria" wrote:

I would use COUNTIF() instead..

Sub DeleteColumnswithZeros()
Dim lngCol As Long, lngLastCol As Long

lngLastCol = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
For lngCol = lngLastCol To 1 Step -1
If WorksheetFunction.CountIf(Columns(lngCol), 0) + _
WorksheetFunction.CountBlank(Columns(lngCol)) = _
Rows.Count Then Columns(lngCol).Delete
Next
End Sub


--
Jacob (MVP - Excel)


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks

  #9   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default Remove columns with all zeros

Thanks again Jacob. I applied the macro below and it didn't do anything. I
also applied the macro Gord Dibben suggested and it removed all columns
totaling zero. The only problem is that it also removed columns containing
text such as Names, Job Title, etc.

Can you suggest how I can tweak the macro below so it disregards columns
with text and dates and looks at only the numbers?

Again really apperciate 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


"Jacob Skaria" wrote:

If you are new to macros..

--Set the Security level to low/medium in (Tools|Macro|Security).
--From workbook launch VBE using short-key Alt+F11.
--From menu 'Insert' a module and paste the below code.
--Get back to Workbook.
--Run macro from Tools|Macro|Run <selected macro()


--I created the macro below in a separate workbook and then opened it in the
workbook I needed to apply the macro. Should I have created the macro in the
workbook that needed the columns removed?

No need; the workbook in which you need to delete the columns should be the
active workbook..

--Also, did I need to include a range in the macro below or was it ok to
copy as is? Appreciate the assistance.

No need; the macro identifies the last column with values and check for zero
cells upto that column.

--
Jacob (MVP - Excel)


"Nora_GG" wrote:

Thank you Jacob. I am pretty new to building macros. I created the macro
below in a separate workbook and then opened it in the workbook I needed to
apply the macro. Should I have created the macro in the workbook that needed
the columns removed? Also, did I need to include a range in the macro below
or was it ok to copy as is? Appreciate the assistance.

By the way, I created the macro below by selecting the Macro option under
Tools.

Thanks again.

"Jacob Skaria" wrote:

I would use COUNTIF() instead..

Sub DeleteColumnswithZeros()
Dim lngCol As Long, lngLastCol As Long

lngLastCol = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
For lngCol = lngLastCol To 1 Step -1
If WorksheetFunction.CountIf(Columns(lngCol), 0) + _
WorksheetFunction.CountBlank(Columns(lngCol)) = _
Rows.Count Then Columns(lngCol).Delete
Next
End Sub


--
Jacob (MVP - Excel)


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks

  #10   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 22,906
Default Remove columns with all zeros

Gord Dibben never suggested a macro.

Gord told you where to store the macro that Gary's Student had posted for
you.


Gord Dibben MS Excel MVP

On Thu, 3 Jun 2010 10:13:51 -0700, Nora_GG
wrote:

Thanks again Jacob. I applied the macro below and it didn't do anything. I
also applied the macro Gord Dibben suggested and it removed all columns
totaling zero. The only problem is that it also removed columns containing
text such as Names, Job Title, etc.

Can you suggest how I can tweak the macro below so it disregards columns
with text and dates and looks at only the numbers?

Again really apperciate 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


"Jacob Skaria" wrote:

If you are new to macros..

--Set the Security level to low/medium in (Tools|Macro|Security).
--From workbook launch VBE using short-key Alt+F11.
--From menu 'Insert' a module and paste the below code.
--Get back to Workbook.
--Run macro from Tools|Macro|Run <selected macro()


--I created the macro below in a separate workbook and then opened it in the
workbook I needed to apply the macro. Should I have created the macro in the
workbook that needed the columns removed?

No need; the workbook in which you need to delete the columns should be the
active workbook..

--Also, did I need to include a range in the macro below or was it ok to
copy as is? Appreciate the assistance.

No need; the macro identifies the last column with values and check for zero
cells upto that column.

--
Jacob (MVP - Excel)


"Nora_GG" wrote:

Thank you Jacob. I am pretty new to building macros. I created the macro
below in a separate workbook and then opened it in the workbook I needed to
apply the macro. Should I have created the macro in the workbook that needed
the columns removed? Also, did I need to include a range in the macro below
or was it ok to copy as is? Appreciate the assistance.

By the way, I created the macro below by selecting the Macro option under
Tools.

Thanks again.

"Jacob Skaria" wrote:

I would use COUNTIF() instead..

Sub DeleteColumnswithZeros()
Dim lngCol As Long, lngLastCol As Long

lngLastCol = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
For lngCol = lngLastCol To 1 Step -1
If WorksheetFunction.CountIf(Columns(lngCol), 0) + _
WorksheetFunction.CountBlank(Columns(lngCol)) = _
Rows.Count Then Columns(lngCol).Delete
Next
End Sub


--
Jacob (MVP - Excel)


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks




  #11   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default Remove columns with all zeros

My apologies Gord Dibben.

"Gord Dibben" wrote:

Gord Dibben never suggested a macro.

Gord told you where to store the macro that Gary's Student had posted for
you.


Gord Dibben MS Excel MVP

On Thu, 3 Jun 2010 10:13:51 -0700, Nora_GG
wrote:

Thanks again Jacob. I applied the macro below and it didn't do anything. I
also applied the macro Gord Dibben suggested and it removed all columns
totaling zero. The only problem is that it also removed columns containing
text such as Names, Job Title, etc.

Can you suggest how I can tweak the macro below so it disregards columns
with text and dates and looks at only the numbers?

Again really apperciate 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


"Jacob Skaria" wrote:

If you are new to macros..

--Set the Security level to low/medium in (Tools|Macro|Security).
--From workbook launch VBE using short-key Alt+F11.
--From menu 'Insert' a module and paste the below code.
--Get back to Workbook.
--Run macro from Tools|Macro|Run <selected macro()


--I created the macro below in a separate workbook and then opened it in the
workbook I needed to apply the macro. Should I have created the macro in the
workbook that needed the columns removed?

No need; the workbook in which you need to delete the columns should be the
active workbook..

--Also, did I need to include a range in the macro below or was it ok to
copy as is? Appreciate the assistance.

No need; the macro identifies the last column with values and check for zero
cells upto that column.

--
Jacob (MVP - Excel)


"Nora_GG" wrote:

Thank you Jacob. I am pretty new to building macros. I created the macro
below in a separate workbook and then opened it in the workbook I needed to
apply the macro. Should I have created the macro in the workbook that needed
the columns removed? Also, did I need to include a range in the macro below
or was it ok to copy as is? Appreciate the assistance.

By the way, I created the macro below by selecting the Macro option under
Tools.

Thanks again.

"Jacob Skaria" wrote:

I would use COUNTIF() instead..

Sub DeleteColumnswithZeros()
Dim lngCol As Long, lngLastCol As Long

lngLastCol = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
For lngCol = lngLastCol To 1 Step -1
If WorksheetFunction.CountIf(Columns(lngCol), 0) + _
WorksheetFunction.CountBlank(Columns(lngCol)) = _
Rows.Count Then Columns(lngCol).Delete
Next
End Sub


--
Jacob (MVP - Excel)


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks


.

  #12   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10
Default Remove columns with all zeros

Hi Jacob, I received additional assistance from Gary's Student and was able
to tweak the macro to provide me with the desired results. Thank you again.

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



"Jacob Skaria" wrote:

If you are new to macros..

--Set the Security level to low/medium in (Tools|Macro|Security).
--From workbook launch VBE using short-key Alt+F11.
--From menu 'Insert' a module and paste the below code.
--Get back to Workbook.
--Run macro from Tools|Macro|Run <selected macro()


--I created the macro below in a separate workbook and then opened it in the
workbook I needed to apply the macro. Should I have created the macro in the
workbook that needed the columns removed?

No need; the workbook in which you need to delete the columns should be the
active workbook..

--Also, did I need to include a range in the macro below or was it ok to
copy as is? Appreciate the assistance.

No need; the macro identifies the last column with values and check for zero
cells upto that column.

--
Jacob (MVP - Excel)


"Nora_GG" wrote:

Thank you Jacob. I am pretty new to building macros. I created the macro
below in a separate workbook and then opened it in the workbook I needed to
apply the macro. Should I have created the macro in the workbook that needed
the columns removed? Also, did I need to include a range in the macro below
or was it ok to copy as is? Appreciate the assistance.

By the way, I created the macro below by selecting the Macro option under
Tools.

Thanks again.

"Jacob Skaria" wrote:

I would use COUNTIF() instead..

Sub DeleteColumnswithZeros()
Dim lngCol As Long, lngLastCol As Long

lngLastCol = ActiveSheet.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
For lngCol = lngLastCol To 1 Step -1
If WorksheetFunction.CountIf(Columns(lngCol), 0) + _
WorksheetFunction.CountBlank(Columns(lngCol)) = _
Rows.Count Then Columns(lngCol).Delete
Next
End Sub


--
Jacob (MVP - Excel)


"Nora_GG" wrote:

How can I remove columns that contain all zeros? Is there a macro? I craeted
a sumif formula to flag columns subtotaling zero but I need a method to
remove these columns. Thanks

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
Remove columns containing all zeros Nora_GG Excel Worksheet Functions 2 June 1st 10 10:44 PM
Remove leading zeros Machel Excel Discussion (Misc queries) 9 February 14th 08 12:07 AM
remove preceding zeros Bruce Excel Worksheet Functions 2 July 27th 07 02:52 PM
REMOVE LEADING ZEROS ichihina Excel Worksheet Functions 2 March 14th 07 07:58 PM
Remove zeros xgirl Excel Worksheet Functions 8 April 13th 05 01:18 PM


All times are GMT +1. The time now is 07:58 AM.

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"