用于擦除带有零和空格的行的代码将不起作用

问题描述 投票:0回答:1

此代码不起作用,它不会从 AO 列中获取最后一行,也不会删除这些行。有人可以帮忙吗?

Sub DeleteRowsWithZeroOrBlankInColumnA() 
    Dim ws As Worksheet 
    Dim cell As Range 
    Dim lastRow As Long      

    On Error Resume Next ' Ignore errors temporarily      

    ' Specify the worksheet 

    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name     

    If Not ws Is Nothing Then 
        ' Determine the last row in column AO 
         lastRow = ws.Cells(ws.Rows.Count, AO).End(xlUp).Row 
        Debug.Print lastRow 
        ' Loop through each cell in column AO from the last row to the first row 
         For i = lastRow To 1 Step -1 
        ' Check if the value in column AO is zero or blank 
        If ws.Cells(i, "AO").Value = 0 Or ws.Cells(i, "AO").Value = "" Then
            ' Delete the entire row if the condition is met 
            ws.Rows(i).EntireRow.Delete 
        End If
    Next i 
    Else 
        Debug.Print "Worksheet not found." ' Print message if worksheet is not found 
    End If  
    On Error GoTo 0 ' Reset error handling 
End Sub 

我尝试使用 43 unstead AO 更改最后一行的代码,删除行的不同变体,但它不起作用

excel vba
1个回答
0
投票

我建议进行这些更正,以使其正常运行:

  • 您在循环中使用名为 i 的变量,但您从未声明它。 添加:

    暗淡我只要

  • 设置lastRow 时,您缺少 AO 列的“”。 改变:

    lastRow = ws.Cells(ws.Rows.Count, "AO").End(xlUp).Row

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.