循环没有做错误,excel vba

时间:2015-07-02 15:08:23

标签: excel vba excel-vba

我正在尝试阅读一个宏,它将总结与一系列日期相关的相应值。 IE- 1月1日将有20行,相应的条目分别为20,50,80。我想要一个将所有这些条目相加的宏,并且一旦所有1月1日的条目相加,那么它将继续到1月2日,并将所有这些条目相加。到目前为止我写的代码给了我一个没有错误的循环。

Private Sub CommandButton1_Click()

Dim dateCheck As String
Dim shipDay As Date
Dim L As Integer
Dim i As Integer
Dim S As Integer
Dim search As String
Dim usaTotal As Long
Dim usaCredit As Long
Dim usaDebit As Long


L = 10
i = 3


dateCheck = InputBox("What Date is Ship Day 1?", "Ship Day Entry")

If IsDate(dateCheck) Then shipDay = DateValue(dateCheck) _
Else: MsgBox ("Invalid Date") ' Prompts user for ship day 1, and checks if actual date




While Not Worksheets("Sheet1").Cells(i, 8).Value = "" ' Runs until all cell rows are accounted for

For S = 0 To 29

shipDay = shipDay + S

Do Until shipDay <> Worksheets("Sheet1").Cells(i, 8).Value ' Execute until new date

search = Worksheets("sheet1").Cells(i, 12).Value ' Variable to use InStr to check for "CAN"

If (((shipDay = Worksheets("Sheet1").Cells(i, 8).Value) And _
InStr(1, search, "CAN", vbBinaryCompare) = 0) _
And (Worksheets("Sheet1").Cells(i, 6).Text = "Invoice")) Then

'Check that date matches, and that it isn't Canada, and that order is an invoice

usaDebit = Worksheets("Sheet1").Cells(i, 22).Value ' Account for Debits
usaCredit = Worksheets("Sheet1").Cells(i, 24).Value ' Account for Credits

usaTotal = usaTotal + usaCredit - usaDebit  ' Calculate contribution

i = i + 1

End If

Loop

MsgBox (usaTotal)


Next S

Worksheets("JUNE Canada").Cells(i, 22).Value = usaTotal

MsgBox (usaTotal)

 ' Need code here that will input final usaTotal into respective space

MsgBox (usaTotal)

Wend ' End of Initial "while not"

End Sub

1 个答案:

答案 0 :(得分:2)

看起来你错过了顶部的“end if”语句。这似乎对我有用(如果输入无效日期,我还添加了一个“Exit Sub”。这似乎有意义,但如果无效日期不影响其余代码,你应该把它拿出来):

Private Sub CommandButton1_Click()

    Dim dateCheck As String
    Dim shipDay As Date
    Dim L As Integer
    Dim i As Integer
    Dim S As Integer
    Dim search As String
    Dim usaTotal As Long
    Dim usaCredit As Long
    Dim usaDebit As Long

    L = 10
    i = 3

    dateCheck = InputBox("What Date is Ship Day 1?", "Ship Day Entry")

    If IsDate(dateCheck) Then
        shipDay = DateValue(dateCheck)
    Else:
        MsgBox ("Invalid Date")
        Exit Sub
    End If ' Prompts user for ship day 1, and checks if actual date

    While Not Worksheets("Sheet1").Cells(i, 8).Value = "" ' Runs until all cell rows are accounted for

        For S = 0 To 29
            shipDay = shipDay + S
            Do Until shipDay <> Worksheets("Sheet1").Cells(i, 8).Value ' Execute until new date

                search = Worksheets("sheet1").Cells(i, 12).Value ' Variable to use InStr to check for "CAN"

                If (((shipDay = Worksheets("Sheet1").Cells(i, 8).Value) And _
                InStr(1, search, "CAN", vbBinaryCompare) = 0) _
                And (Worksheets("Sheet1").Cells(i, 6).Text = "Invoice")) Then

                'Check that date matches, and that it isn't Canada, and that order is an invoice

                usaDebit = Worksheets("Sheet1").Cells(i, 22).Value ' Account for Debits
                usaCredit = Worksheets("Sheet1").Cells(i, 24).Value ' Account for Credits

                usaTotal = usaTotal + usaCredit - usaDebit  ' Calculate contribution

                i = i + 1

                End If

            Loop

            MsgBox (usaTotal)
        Next S

        Worksheets("JUNE Canada").Cells(i, 22).Value = usaTotal
        MsgBox (usaTotal)

         ' Need code here that will input final usaTotal into respective space

        MsgBox (usaTotal)

    Wend ' End of Initial "while not"
End Sub