2016-07-22 59 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 
相关问题