2014-04-17 32 views
0

我的問題是我在Excel中有一個電視程序。廣播公司決定在上午8:00改變日期。我想在凌晨4點之後改變一天。在特殊情況下我怎麼能這樣做,就像沒有任何節目從04:xx開始,但只有05:xx或06:00。 到目前爲止,我想出了這個代碼,但是如果我有在04:00開始的程序,那就好了。VBA在列中指定時間後更改日期

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

您應該使用Hour(time)函數。

而不是

If InStr(airingTime, "04:") Then 

試試這個:

If Hour(airingTime) >=4 Then 


編輯1.

修改這樣的行:

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

此外,在Do Until ActiveCell.Value = "END"之後移動行airingDate = ActiveCell.Value

+0

這將是一件好事,但是它不會在4點以後的每個小時進行插入嗎?它會在5和6以及7 ...處插入等等。這張表包含很多天。如果節目從4點開始或稍後開始,我希望它每天都插入一行每一個新的日期。然後從04:00到下一個04:00這將是一天。 – molnardenes