简体   繁体   English

Excel VBA - 删除时间值小于的单元格

[英]Excel VBA - delete cell with time value less then

I'm trying to delete specific range based on a time value in the column "J".我正在尝试根据“J”列中的时间值删除特定范围。 So far I got this:到目前为止,我得到了这个:

Dim wb As Workbook
Dim c As Range
Dim zakres As Range
Dim zakres2 As Range
Dim all As String
Dim all2 As Range
Dim ile As String
Dim czas As Range

Set wb = ThisWorkbook
Set czas = wb.Worksheets("Dane").Range("J2")

ile = Application.WorksheetFunction.CountA(wb.Worksheets("Dane").Range("L:L"))

For i = 1 To ile
    If czas.Value < "00:01:00" Then
        Set zakres = czas.Offset(0, 0)
        Set zakres2 = czas.Offset(0, 2)
        all = zakres.Address & ":" & zakres2.Address
        Set all2 = Range(all)
        all2.Delete Shift:=xlUp
    Else
        Set czas = czas.Offset(1, 0)
    End If
Next i

In the line If czas.Value < "00:01:00" Then I'm getting the 424 run time error - Object required. If czas.Value < "00:01:00" Then我收到 424 运行时错误 - 需要 Object。 It confuses me, since the variable czas is already declared...这让我很困惑,因为变量czas已经被声明了......

Any ideas why it's happening and how to deal with it?任何想法为什么会发生以及如何处理它?

When you delete the row that contains the range czas you also delete that range object.当您删除包含范围czas的行时,您也会删除该范围 object。 A null range has no property .value which is why you are getting an object required error. null 范围没有属性.value这就是为什么您会收到 object required 错误。

A good way to mass delete range object is to use union to create non-contiguous ranges and then delete them all at once.批量删除范围 object 的一个好方法是使用union创建非连续范围,然后一次将它们全部删除。 This spares you the weirdness of shifting rows in a loop and also will significantly improve speed as deleting is pretty expensive.这样可以避免在循环中移动行的怪异之处,并且还会显着提高速度,因为删除非常昂贵。

Dim wb As Workbook
Dim c As Range
Dim zakres As Range
Dim zakres2 As Range
Dim ile As String
Dim czas As Range
Dim i As Long
Dim delrng As Range

Set wb = ThisWorkbook
Set czas = wb.Worksheets("Dane").Range("J2")

ile = Application.WorksheetFunction.CountA(wb.Worksheets("Dane").Range("L:L"))

For i = 1 To ile
    If czas.Value < "00:01:00" Then
        Set zakres = czas.Offset(0, 0)
        Set zakres2 = czas.Offset(0, 2)
        If delrng Is Nothing Then
            Set delrng = Range(zakres, zakres2)
        Else
            Set delrng = Union(delrng, Range(zakres, zakres2))
        End If
    End If
    Set czas = czas.Offset(1, 0)
Next i
If Not delrng Is Nothing Then
    delrng.Delete
end if

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

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