Excel VBA循环问题

我正在编写一个以input有效date开始的macros。 date条目后,我然后希望macrossearch具有此date的所有行的工作表。 然后,我希望所有这些行的积分和借记总额,然后我想把这些总额放在另一个工作表中。 这是我为此编写的代码。

问题是EVENTUALLY,代码会在“Search =,If,End If”中无限循环。 如果我在结束之前放置一个“Else:Next I”,那么我得到一个错误,否则不提示。

有什么build议么?

Private Sub CommandButton3_Click() Dim dateCheck As String Dim lastRow As Long Dim L As Integer Dim I As Long Dim shipDay As Date Dim search As String Dim usaTotal As Long Dim usaCredit As Long Dim usaDebit As Long 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 For L = 0 To 30 ' Execute this code for 1 month worth of dates shipDay = shipDay + L MsgBox shipDay For I = 1 To 10000 ' Check every row of the worksheet search = Worksheets("sheet1").Cells(I, 12).Value ' Variable to use InStr to check for "CAN" If ((InStr(1, search, "CAN", vbBinaryCompare) = 0) _ And (Worksheets("Sheet1").Cells(I, 8) = shipDay) _ And (Worksheets("Sheet1").Cells(I, 6).Text = "Invoice")) Then 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 ' This is where I tried placing an Else: Next I, which gives me an else without for prompts. End If Next I MsgBox (usaTotal) Worksheets("JUNE canada").Cells(L + 10, 4).Value = usaTotal / 1000 'Enter value into sheet usaTotal = 0 ' Reset usaTotal value Next L 

使用使用的范围,而不是10000

  Dim lROw as Long Do While lRow <= ws.UsedRange.Rows.Count 'Do stuff here. lRow = lRow + 1 ws.Range("A" & lRow).Activate Loop 

所以你的代码看起来像这样

 Private Sub CommandButton3_Click() Dim dateCheck As String Dim lastRow As Long Dim L As Integer Dim I As Long Dim shipDay As Date Dim search As String Dim usaTotal As Long Dim usaCredit As Long Dim usaDebit As Long 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 For L = 0 To 30 ' Execute this code for 1 month worth of dates shipDay = shipDay + L MsgBox shipDay I = 1 Do While I <= ws.UsedRange.Rows.Count search = Worksheets("sheet1").Cells(I, 12).Value ' Variable to use InStr to check for "CAN" If ((InStr(1, search, "CAN", vbBinaryCompare) = 0) _ And (Worksheets("Sheet1").Cells(I, 8) = shipDay) _ And (Worksheets("Sheet1").Cells(I, 6).Text = "Invoice")) Then 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 ' This is where I tried placing an Else: Next I, which gives me an else without for prompts. elseif instr(something, something) then End If I = I + 1 ws.Range("A" & I).Activate Loop MsgBox (usaTotal) Worksheets("JUNE canada").Cells(L + 10, 4).Value = usaTotal / 1000 'Enter value into sheet usaTotal = 0 ' Reset usaTotal value Next L