设置代码的方法是什么,以便在A列中查找最后一个填充的行,然后消除列H具有值= 0的行?

发布于 2025-01-26 11:20:20 字数 1158 浏览 3 评论 0原文

早上好,

目前,我有此代码可以删除行没有到期日(J列)和付款金额= 0(H列)。

Sub delete_rows()

Range("A1").End(xlDown).Select
    
Sheets("AA").Select
Range("J2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-5]=0,"""",RC[-5])"
Range("J2").Select
Selection.AutoFill Destination:=Range("J2:J500"), Type:=xlFillDefault
Range("J2").End(xlDown).Select
Range("K2").Select

Application.ScreenUpdating = False
With Sheet2
 For line = .Cells(.Rows.Count, "J").End(xlUp).Row To 2 Step -1
   If .Cells(line, "J") = "" Then
     .Rows(line).Delete
   End If
 Next linha
End With
Application.ScreenUpdating = True

ActiveCell.FormulaR1C1 = "=IF(RC[-4]="""","""",RC[-4])"
Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K500"), Type:=xlFillDefault
Range("K2").End(xlDown).Select
Range("J1").Select

Application.ScreenUpdating = False
With Sheet2
 For line = .Cells(.Rows.Count, "K").End(xlUp).Row To 2 Step -1
   If .Cells(line, "K") = "" Then
     .Rows(line).Delete
   End If
 Next line
End With
Application.ScreenUpdating = True
End sub()

我创建了一个具有定义数量的代码...但是,该代码运行需要很长时间,因为有时行数很少,并且总是运行500行。设置代码的方法是什么,以便它在A列中查找最后一个填充的行,然后消除列H具有值= 0的行,而列j n no值中的行?

Good morning,

currently I have this code to delete rows without due date (Column J) and amount paid=0 (Column H).

Sub delete_rows()

Range("A1").End(xlDown).Select
    
Sheets("AA").Select
Range("J2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-5]=0,"""",RC[-5])"
Range("J2").Select
Selection.AutoFill Destination:=Range("J2:J500"), Type:=xlFillDefault
Range("J2").End(xlDown).Select
Range("K2").Select

Application.ScreenUpdating = False
With Sheet2
 For line = .Cells(.Rows.Count, "J").End(xlUp).Row To 2 Step -1
   If .Cells(line, "J") = "" Then
     .Rows(line).Delete
   End If
 Next linha
End With
Application.ScreenUpdating = True

ActiveCell.FormulaR1C1 = "=IF(RC[-4]="""","""",RC[-4])"
Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K500"), Type:=xlFillDefault
Range("K2").End(xlDown).Select
Range("J1").Select

Application.ScreenUpdating = False
With Sheet2
 For line = .Cells(.Rows.Count, "K").End(xlUp).Row To 2 Step -1
   If .Cells(line, "K") = "" Then
     .Rows(line).Delete
   End If
 Next line
End With
Application.ScreenUpdating = True
End sub()

I created a code with a defined number of lines...however it takes a long time for the code to run, because sometimes the number of lines is small and it always runs the 500 lines. What's the way to set the code so that it looks for the last filled row in column A, and then eliminate the rows where column H has values =0 and in column J no values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2025-02-02 11:20:20

请检查:找到最后一个单元格a>。还请查看:避免选择

之后,我认为您应该能够理解以下代码,这应该为您带来所需的结果:

Sub test()

Application.ScreenUpdating = False

'declare your variables
Dim ws As Worksheet
Dim Rng1 As Range, Rng2 As Range
Dim i As Long, lastRow As Long

Set ws = Sheets("AA")

With ws

    'get last row in
    lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
    'set ranges for loop
    Set Rng1 = Range(.Cells(2, "H"), .Cells(lastRow, "H"))
    Set Rng2 = Range(.Cells(2, "J"), .Cells(lastRow, "J"))
    
    'reverse loop
    For i = Rng1.Rows.Count To 1 Step -1

        'check conditions for cell in "H" and "J"
        If Rng1.Cells(i) = 0 And Rng2.Cells(i) = "" Then
        
            'defined ranges start at row 2, hence +1
            ws.Rows(i + 1).Delete
        
        End If

    Next i
    
End With

Application.ScreenUpdating = True

End Sub

Please check: find last cell. Also have a look at: avoid select.

Afterwards, I think you should be able to understand the following code, which should get you the required result:

Sub test()

Application.ScreenUpdating = False

'declare your variables
Dim ws As Worksheet
Dim Rng1 As Range, Rng2 As Range
Dim i As Long, lastRow As Long

Set ws = Sheets("AA")

With ws

    'get last row in
    lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
    'set ranges for loop
    Set Rng1 = Range(.Cells(2, "H"), .Cells(lastRow, "H"))
    Set Rng2 = Range(.Cells(2, "J"), .Cells(lastRow, "J"))
    
    'reverse loop
    For i = Rng1.Rows.Count To 1 Step -1

        'check conditions for cell in "H" and "J"
        If Rng1.Cells(i) = 0 And Rng2.Cells(i) = "" Then
        
            'defined ranges start at row 2, hence +1
            ws.Rows(i + 1).Delete
        
        End If

    Next i
    
End With

Application.ScreenUpdating = True

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文