试图让我的 if 语句在 00:40:00 的持续时间内复制并粘贴“午休时间”

Trying to get my if statement to copy and paste "lunch Breaks" over duration of 00:40:00

提问人:Jesse Cantrell 提问时间:11/16/2023 最后编辑:Mayukh BhattacharyaJesse Cantrell 更新时间:11/16/2023 访问量:42

问:

第一部分是有效的,但这部分很容易。时间的持续时间让我陷入了困境。双关 我试过将格式持续时间更改为十进制,但它使它变得丑陋。

 Sub Long_Lunch()


lastrow = Worksheets("Raw Data").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow




If Worksheets("Raw Data").Cells(i, 4).Value = "Lunch Break" Then
    If Worksheets("Raw Data").Cells(i, 6).Value > "00:40:00" Then
    
    Worksheets("Raw Data").Rows(i).Copy
    Worksheets("D").Activate
    b = Worksheets("D").Cells(Rows.Count, 1).End(xlUp).Row
    Worksheets("D").Cells(b + 1, 1).Select
    ActiveSheet.Paste
   End If
End If


Next



Application.CutCopyMode = False
Worksheets("Raw Data").Activate
Worksheets("Raw Data").Cells(1, 1).Select
MsgBox ("Done")


End Sub
Excel VBA

评论

0赞 SeanC 11/16/2023
时间(和日期)在电子表格上以数字形式存储 - 在电子表格上为 0.277777 (40/1440)00:40:00

答:

0赞 taller 11/16/2023 #1
  • 不需要为某个区域激活目标工作表。Copy
Option Explicit

Sub Long_Lunch()
    Dim lastRow As Long, lastRowD As Long
    Dim i As Long, Sht As Worksheet
    Set Sht = Worksheets("D")
    With Worksheets("Raw Data")
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        For i = 2 To lastRow
            If .Cells(i, 4).Value = "Lunch Break" Then
                If Format(.Cells(i, 6).Value, "hh:mm:ss") > "00:40:00" Then
                    lastRowD = Sht.Cells(Sht.Rows.Count, 1).End(xlUp).Row
                    Application.CutCopyMode = False
                    .Rows(i).Copy Sht.Cells(lastRowD + 1, 1)
                End If
            End If
        Next
    End With
    Application.CutCopyMode = False
    MsgBox ("Done")
End Sub

评论

0赞 Jesse Cantrell 11/16/2023
像魅力一样工作,而且速度更快。那是因为设计工作表未激活吗?我刚刚开始学习 vba,所以肯定有很多东西要学
0赞 taller 11/16/2023
@JesseCantrell值得一读 如何避免在Excel VBA中使用选择