2016-07-22 58 views
-1

因此,在Excel Sheet1中我有一個用戶需要的信息列表(有一張圖片)在那裏我們有開始時間和執行時間。這些信息被傳遞給sheet3,我希望結束時間根據執行時間而改變。例如,如果用戶輸入開始時間= 1:00 pm並且執行時間= 30分鐘。我希望代碼在表格3 = 1:30 pm的最後時間。下面是當前代碼我有:enter image description here我需要一段時間來改變用戶使用VBA輸入的內容

Sub findData() 
    Dim workflow As String 
    Dim finalrow As Integer 
    Dim i As Integer 

    With Sheets("Sheet1") 
     workflow = .Range("C5").Value 
     servergri = .Range("C9").Value 
     gridf = .Range("C9").Value 
     StartTime = .Range("c11").Value 
    End With 

    With Sheets("Sheet3") 
     finalrow = .Range("C" & Rows.Count).End(xlUp).Row 

     For i = 5 To finalrow 
      If .Cells(i, 3) = workflow And (.Cells(i, 4) = servergri Or .Cells(i, 5) = gridf) Then 

       .Rows(i).Insert 
       'Add new information to the new row. 
       'The new row number is still = i 

       .Cells(i, 3) = workflow 
       .Cells(i, 4) = servergri 
       .Cells(i, 6) = StartTime 
        .Cells(i, 3).Resize(2, 4).Interior.ColorIndex = 8 


       'If you only want to add one row then your should exit the loop 
       Exit For 
      End If 
     Next 
    End With 

End Sub 

回答

0

這將做到這一點,但你需要確保用戶只輸入時間(分鐘)。 EG「60」1小時或「15」15分鐘。他們不應該輸入標籤。

Sub findData() 
Dim workflow As String 
Dim finalrow As Integer 
Dim i As Integer 
Dim StartTime as Date 
Dim ExecutionTime as Long 

With Sheets("Sheet1") 
    workflow = .Range("C5").Value 
    servergri = .Range("C9").Value 
    gridf = .Range("C9").Value 
    On Error Goto Next 
    StartTime = .Range("c11").Value 
    If Err Then 
     MsgBox "You didn't enter a valid start time.", vbExclamation 
     Exit Sub 
    End If 
    ExecutionTime = .Range("c16").Value 
    If Err Then 
     MsgBox "You didn't enter a valid execution time.", vbExclamation 
     Exit Sub 
    End If 
    On Error Goto 0 
End With 

With Sheets("Sheet3") 
    finalrow = .Range("C" & Rows.Count).End(xlUp).Row 

    For i = 5 To finalrow 
     If .Cells(i, 3) = workflow And (.Cells(i, 4) = servergri Or .Cells(i, 5) = gridf) Then 
      .Rows(i).Insert 
      'Add new information to the new row. 
      'The new row number is still = i 

      .Cells(i, 3) = workflow 
      .Cells(i, 4) = servergri 
      .Cells(i, 6) = StartTime 
       .Cells(i, 3).Resize(2, 4).Interior.ColorIndex = 8 

      'You don't mention where this time should go on Sheet 3, so I used Cell(i, 9) 
      'TimeSerial(Hours, Minutes, Seconds) 
      .Cells(I, 9).Value = StartTime + TimeSerial(0, ExecutionTime, 0) 
      .Cells(I, 9).NumberFormat = "hh:mm" 

      'If you only want to add one row then your should exit the loop 
      Exit For 
     End If 
    Next 
End With 
End Sub 
相關問題