错误1004应用程序定义或对象定义的错误

时间:2019-05-31 09:04:12

标签: excel vba

代码在这里:

Sub deleterow2()
Dim a As Integer
Dim n As Integer
Dim c As Integer

LastRow = Range("F" & Rows.Count).End(xlUp).Row
For n = 0 To LastRow
a = 1
c = 0
Do Until c = 1
Cells(n + a, 6).Select
If Selection.value = Cells(n, 6) And Selection.value > 30 Then
Selection.EntireRow.Delete
Else
c = 1
End If
a = a + 1
Loop
Next n
End Sub

这怎么了?

3 个答案:

答案 0 :(得分:1)

请参见下面的更正代码...而不是将n从0开始,而将a从0开始。另外,请避免使用.Select所有...并且您应该尝试完全声明您的范围:

Sub deleterow222()

Dim lastRow As Long, R As Long
Dim ws As Worksheet: Set ws = ActiveWorkbook.ActiveSheet
Dim idColumn as Long: idColumn = 6
Dim diffColumn as Long: diffColumn = 19

    lastRow = ws.Range("F" & ws.Rows.Count).End(xlUp).Row
    For R = lastRow To 2 Step -1
        With ws
            If .Cells(R, idColumn) = .Cells(R - 1, idColumn) And .Cells(R, diffColumn) > 30 Then
                .Cells(R, idColumn).EntireRow.Delete
            End If
        End With
    Next R

End Sub

答案 1 :(得分:1)

一些提示:

  1. 您无法使用n = 0,因为会创建错误。行从1开始。
  2. 循环删除时,从头开始。 For n = LastRow To 1 Step -1
  3. 如果有很多行,您将使用For Loop声明变量As Long
  4. 如果您有很多行,最好使用Array
  5. 尝试通过创建具有工作表名称的.Select来避免With Statement With ThisWorkbook.Worksheets("Sheet1"),然后使用.Cells(n + A, 6).Value
  6. 引用单元格
  7. Do Until也应该从高到低开始,因为您从下到上。

答案 2 :(得分:0)

enter image description here

sub deleterow2()

Dim lastRow As Long,R As Long 昏暗的ws作为工作表:设置ws = ActiveWorkbook.ActiveSheet

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='mailler']//li[contains(@class, 'mail')]//a"))).click()

结束子