VBA在列中指定的时间后更改日期

时间:2014-04-17 18:37:48

标签: excel vba

我的问题是我在Excel中有一个电视节目。广播公司决定在上午8点改变日期。我想在凌晨4点之后改变一天。我怎么能在特殊情况下这样做,比如没有任何节目从04:xx开始,但只有05:xx或06:00。 到目前为止,我想出了这个代码,但是如果我的程序在04:00开始,它就会很好。

Sub Gomb1_Kattintás()
    Dim content As String
    Dim airingDate As Date
    Dim airingTime As String

    Range("A2").Select
    airingDate = ActiveCell.Value
    ActiveCell.Offset(1, 0).EntireRow.Insert
    Do Until ActiveCell.Value = "END"        
        airingTime = Format(ActiveCell.Offset(, 1), "hh:mm")
        content = ActiveCell.Offset(, 2).Value

        If InStr(airingTime, "04:") Then
            airingDate = DateAdd("d", 1, airingDate)
            ActiveCell.EntireRow.Resize(3).Insert
            ActiveCell.Offset(1, 0).Value = airingDate
            ActiveCell.Offset(3, 0).Select
        End If      

        If IsEmpty(ActiveCell.Offset(1, 1)) Then
            ActiveCell.Offset(1, 0).EntireRow.Delete
        End If

        If InStr(content, ", ism.") Then
            ActiveCell.Offset(, 6) = 1
            content = Replace(content, ", ism.", "")
            ActiveCell.Offset(, 2).Value = content
        End If
        If InStr(content, ", live") Then
            ActiveCell.Offset(, 7) = 1
            content = Replace(content, ", live", "")
            ActiveCell.Offset(, 2).Value = content
        End If
        If InStr(content, ", HD") Then
            ActiveCell.Offset(, 8) = 1
            content = Replace(content, ", HD", "")
            ActiveCell.Offset(, 2).Value = content
        End If
        If InStr(content, ", premier") Then
            ActiveCell.Offset(, 9) = 1
            content = Replace(content, ", premier", "")
            ActiveCell.Offset(, 2).Value = content
        End If
        ActiveCell.Offset(1, 0).Select
    Loop
End Sub

1 个答案:

答案 0 :(得分:1)

您应该使用Hour(time)功能。

而不是

If InStr(airingTime, "04:") Then

试试这个:

If Hour(airingTime) >=4 Then


编辑1。

修改这样的行:

If Hour(airingTime) >=4 And Hour(airingTime) <= 7 Then

此外,请在airingDate = ActiveCell.Value之后移动行Do Until ActiveCell.Value = "END"