PDA

View Full Version : How to add a column to a collection



troelsi
06-03-2007, 10:40 AM
Hi

Hello

I need to delete columns that doesn't contain data, and in order to do that. I believe I first have to create a collection og those columns that doesn't contain data. This is what I've come up with so far but unfortunately it doesn't work:


dim s as collection
For i = 1 To r

If WorksheetFunction.Sum(Range(Cells(2, i + 1), Cells(p + 1, i + 1))) = 0 Then
s.Add (Columns(i + 1))
End If
Next i

For Each comlumns In s
Columns.Delete Shift:=xlToLeft
Next

Bob Phillips
06-03-2007, 10:57 AM
You don't need a collection, in fact it must be the last thing you need, you can either delete inline, or else accumulate the rows and delete.

But what is r, what is p, and what is the condition that triggers the delete?

troelsi
06-03-2007, 11:26 AM
p is the number of rows and r is the number of columns.

If a column sums to zero it must be deleted.

I don't know what it means to delete inline or how to accumulate the columns.

mdmackillop
06-03-2007, 12:37 PM
This will delete the columns to the extent of the range. If you want the whole column deleted, let us know.
Sub DelCols()
For i = r To 1 Step -1
If WorksheetFunction.Sum(Range(Cells(2, i + 1), Cells(p + 1, i + 1))) = 0 Then
Range(Cells(2, i + 1), Cells(p + 1, i + 1)).Delete shift:=xlToLeft
End If
Next
End Sub

troelsi
06-03-2007, 01:15 PM
Thanks.

I ended up using the union() function instead.