如何将消息框打印到列?

时间:2019-02-11 11:02:56

标签: excel vba

当前,我有一个消息框,以分钟为单位输出两个日期之间的时差。我想更改此设置,以便将结果打印在与'StartTime'内联的Coloumn“ O”中。

任何建议都值得赞赏!

Private Sub CommandButton24_Click()
    Dim Transmit As Boolean
    Dim StartTime As Date
    Dim EndTime As Date
    Dim RunTime As Date
    Dim messagebox As String
    Dim i As Integer

    i = 0
    'conditions for startTime
    While Cells(i + 12, 1) = "Time"
        If (Cells(i + 12, 6) = " Active" And Cells(i + 12, 8) = " False" And Transmit = False) Then
            Transmit = True
            StartTime = CDate(Cells(i + 12, 2))
        End If

        'conditions for endTime
        If ((Cells(i + 12, 6) = " Standby" Or Cells(i + 12, 6) = " Shutdown" Or Cells(i + 12, 8) = " True") And Transmit = True) Then
            EndTime = Cells(i + 12, 2)
            Transmit = False         
        End If            

        If (Cells(i + 12, 6) = "Active" And Cells(i + 12, 8) = "True" And Transmit = False) Then

        Else

        End If

        If StartTime = 0 Or EndTime = 0 Then

        Else
            messagebox = MsgBox((DateDiff("n", StartTime, EndTime)), vbOKOnly)
            StartTime = 0
            EndTime = 0
        End If

        i = i + 1
    Wend
End Sub

当前,当我希望将每个结果打印到StartTime的行时,它将每个结果打印到一个消息框中。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

它会记住变量中的StartTimeRow,然后稍后将其用于该行的输出。

此外,由于您始终使用i + 12,因此可以从i = 12开始,然后每次都使用i而不是i + 12

Private Sub CommandButton24_Click()
    Dim Transmit As Boolean
    Dim StartTime As Date
    Dim EndTime As Date
    Dim RunTime As Date
    Dim messagebox As String
    Dim i As Long
    i = 12

    Dim StartTimeRow As Long

    'conditions for startTime
    While Cells(i, 1) = "Time"
        If (Cells(i, 6) = " Active" And Cells(i, 8) = " False" And Transmit = False) Then
            Transmit = True
            StartTime = CDate(Cells(i, 2))
            StartTimeRow = i '### Remember in which row start time was.
        End If

        'conditions for endTime
        If ((Cells(i, 6) = " Standby" Or Cells(i, 6) = " Shutdown" Or Cells(i, 8) = " True") And Transmit = True) Then
            EndTime = Cells(i, 2)
            Transmit = False         
        End If            

        If (Cells(i, 6) = "Active" And Cells(i, 8) = "True" And Transmit = False) Then

        Else

        End If

        If StartTime = 0 Or EndTime = 0 Then

        Else
            Cells(StartTimeRow, "O").Value = DateDiff("n", StartTime, EndTime)
            StartTime = 0
            EndTime = 0
        End If

        i = i + 1
    Wend
End Sub