简体   繁体   English

VBA:检查全为 0 的列并删除

[英]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.我是 VBA 的新手,我一直在努力弄清楚如何做到这一点。 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.此范围包含一个 header,有些列只有我要删除的 0 单元格。 Below is an example:下面是一个例子:

Header1 Header2.标头 1 标头 2。 Header3.标头3。 Header4 1 0. 2. 3 2. 0. 3. 4 3. 0. 4. 5标头 4 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.我希望从第 2 行开始,每一行的代码为 go,并检查行中的每个单元格的值是否为 0。 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.如果它是 0,那么我希望它检查列中的每个单元格以查看列中的所有单元格值是否也为 0。如果是,则删除。 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).上面的工作方式是它将开始检查第 1 行。单元格 (1,1) 的值为 1,因此它会转到下一列单元格 (1,2)。 It checks that the value is 0, and then moves to Cell (2,2) down to Cell (2,3).它检查该值是否为 0,然后移动到 Cell (2,2) 到 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.由于列中每个单元格的所有值均为 0,因此第 2 列被删除,然后转到第 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.它检查列 A 并查看值加起来是否为 0,如果是,则删除该列。 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.如果 A 不加到 0,您只需要让列更改字母即可。希望这对您有所帮助。

 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.该代码将删除所有只有文本、只有 0 值或两者都有的列,但如果其路径中有 Null 列,它将停止。 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: :)如果 Exit Sub 被删除,该过程将永远运行(或直到 pc 崩溃),所以要小心::)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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