Thread: Macro help
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Joshua Fandango Joshua Fandango is offline
Member
 
Location: In a hole in the ground there lived Joshua Fandango
Posts: 30
Default Macro help

Hi Gemz,

The following starts from cell C3 (change if necessary e.g. all "C3"s
to "B2" if that's where the data starts), counts the number of columns
to use from C3 to the right (can't handle any blank cells), tests if
the sum of the column in question = 0 and if so adds it into an array
which is deleted once all columns have been tested.

The array is necessary to catch instances where 2 or more sequential
columns sum 0.

Option Explicit
Sub Delete_Zero_Columns()
Dim NumCols As Integer, i As Integer
Dim StartCol As Range, ColArray As Range
Set StartCol = Range("C3")
NumCols = WorksheetFunction.CountA(Range("C3", Range("C3").End
(xlToRight)))

For i = 0 To NumCols - 1
If WorksheetFunction.Sum(StartCol.Offset(0, i).EntireColumn) = 0
Then
If ColArray Is Nothing Then
Set ColArray = StartCol.Offset(0, i).EntireColumn
Else
Set ColArray = Union(ColArray, StartCol.Offset(0,
i).EntireColumn)
End If
End If
Next i
ColArray.Delete
End Sub

HtH,
JF.

On 18 Dec, 10:51, Gemz wrote:
Hi,

Hope u can help me with the below:

I have a spreadsheet which is exported from a system with a whole lot of 0’s
that don’t need to be there.

So, the condition is – if the contents of C3 down, D3 down, E3 down etc
contains ALL 0 then delete entire column. The spreadsheet spans from about C3
to AH onwards (however the spreadsheet can grow smaller or bigger at any time
so its not good to specify an end column). I need to have this for all the
columns i.e. F3 down, G3 down etc etc.

So, all i want it to do is look from cell 3 downwards in each column and if
the contents of each column it is ALL 0 then to delete the entire column.

I hope this is making sense, what i want isn’t so hard but its not easy to
say it!

Thanks for help.