循环没有做错误,Excel VBA

我正在尝试阅读一个macros,它将总结与一系列date相关的相应值。 IE- 1月1日将有20行,相应的列中有20,50,80的条目。 我想要一个将所有这些条目相加的macros,一旦所有的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 

看起来你错过了顶部的“如果”声明。 这似乎适用于我(如果input的是无效date,我还添加了“Exit Sub”,这似乎是有道理的,但如果无效date不影响其余代码,则应将其取出):

 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