简体   繁体   中英

VBA: Check for Columns with all 0 and delete

I'm new to VBA and I've been struggling to figure out out how to do this. If someone can help, that'll be greatly appreciated.

What I want to do: I have a range of cells that I copied over from another worksheet. This range contains a header and some columns only have cells with 0's in them that I want to delete. Below is an example:

Header1 Header2. Header3. Header4 1 0. 2. 3 2. 0. 3. 4 3. 0. 4. 5

I want the code to go through each row, starting with Row 2, and check each cell in the row if it's value is 0 or not. if it is 0, then I want it to then check each cell in the column to see if all the cells values in the column are also 0. If it is, then delete. If it encounters a value other than zero, then it will jump to the start of the next column and repeat.

How it would work with the above is that it will start checking with Row 1. Cell (1,1) has a value of 1 so then it goes to the next column Cell (1,2). It checks that the value is 0, and then moves to Cell (2,2) down to Cell (2,3). Since all values are 0 for every cell in the column, column 2 is deleted and then it goes to column 3.

This is the code that I have so far but it's not deleting any of the columns. The code goes backwards for the column counter because that's what I read to be the easiest method when deleting columns

Sub Delete column ()
Dim i as long
Dim j as long
Dim lrow as long
Dim lCol as long

lrow = Cells(rows.count, 1).end(xlup).row
lcol = cells(1, column.count).end(xltoleft).column

For i = 2 to lRow
For j = lCol to 8 Step -1
if Cell(I,j) = 0 then

else
Cell.offset(0,1).select
end if 
next j
Column(1).Entirecolumn.delete 
next i

End sub ()

This should help you out a bit. It checks Column A and sees if the values add up to 0 and if they do then it deletes the column. You'll just need to work on getting the column to change letters if A doesn't add to 0. Hopefully this helps you out some.

 Sub Deletecolumn()
Dim ColumnTotal As Double

ColumnTotal = Application.WorksheetFunction.Sum(Columns("A:A"))

If ColumnTotal = 0 Then
Columns(1).EntireColumn.Delete
End If
End Sub

The code will delete all columns which have either text only, 0 values only or both, but it will stop if there is a Null column in its path. It will also inform the user of how many columns were deleted. If Exit Sub is removed, the procedure will run forever (or until pc crashes), so be careful: :)

Sub DeleteColumns()

    Dim ColumnCount As Integer
    
    Dim DeletedColumnCount As Integer
    
    For ColumnCount = 1 To Columns.Count

        If Application.WorksheetFunction.Sum(Columns(ColumnCount)) = 0 Then
        
            Columns(ColumnCount).EntireColumn.Delete
            
            DeletedColumnCount = DeletedColumnCount + 1
            
            ColumnCount = ColumnCount - 1
            
        End If
        
        If Application.WorksheetFunction.CountA(Columns(ColumnCount + 1)) = 0 Then
            
            MsgBox DeletedColumnCount & " invalid columns were deleted!"
            
            Exit Sub
            
        End If

    Next ColumnCount

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM