Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

vba - Excel macro to delete all rows on all worksheets if the value in column AA = 0

I have a workbook containing 197 sheets. I need to delete all rows in each sheet if the value in column AA is zero. Anyone know how to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you would like to delete each row if and only if that row's column AA is zero, then the below should work for you.

Sub delete0rows()
    Dim Worksheet As Excel.Worksheet
    Dim lastRow As Long
    Dim i As Integer
    For Each Worksheet In Application.ThisWorkbook.Worksheets
        lastRow = Worksheet.Cells(Rows.Count, 1).End(xlUp).Row
        i = 1
        Do While i <= lastRow
            If Worksheet.Range("AA" & i).Value = 0 Then
                Worksheet.Rows(i).Delete
                i = i - 1
                lastRow = lastRow - 1
            End If
            i = i + 1
        Loop
    Next
End Sub

Note this will only delete the row if the cell value in AA is 0. There are several subtleties here... Excel will show a 0 even if the cell value is '0 or =0 among other things, and those rows will not be deleted with the above code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...