Excel VBA循环问题
我正在编写一个以输入有效日期开始的宏。输入日期后,我希望宏在工作表中搜索所有具有此日期的行。然后,我希望对这些行的所有贷方和借方进行合计,然后将这些合计放在另一张工作表中。这是我为此编写的代码 问题是,最终,代码将在“Search=、If和End If”中无限循环。如果我在End If之前放置一个“Else:Next I”,那么我将得到一个错误,而没有提示 有什么建议吗Excel VBA循环问题,vba,excel,Vba,Excel,我正在编写一个以输入有效日期开始的宏。输入日期后,我希望宏在工作表中搜索所有具有此日期的行。然后,我希望对这些行的所有贷方和借方进行合计,然后将这些合计放在另一张工作表中。这是我为此编写的代码 问题是,最终,代码将在“Search=、If和End If”中无限循环。如果我在End If之前放置一个“Else:Next I”,那么我将得到一个错误,而没有提示 有什么建议吗 Private Sub CommandButton3_Click() Dim dateCheck As String Dim
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
Dim lROw尽可能长
当IF语句不起作用时,是否继续For循环?好吧,你不能用“下一个i”来进入循环的下一个迭代。保持原样,没有“其他”部分有什么不对?这样,当IF语句不起作用时,它将转到下一个I。问题是“下一个I”不会返回到“对于I=1到10000”。它返回到“search=worksheets”,并无限循环。似乎不是无限循环,为什么你认为它是?尽管效率极低:您不应该使用到1000
,而应该使用到工作表(“sheet1”)。使用drange.Rows.Count
;如果这是您的目标,但即使是For
循环也不是必需的,因为您可以更轻松地使用WorksheetFunction.SumIFS
执行。当“I”变量转到下一个“search=worksheets”时,它是否会增加?它不会无限循环,而是10000次,这可能相当长。特别是如果你做了30次。(下一页)
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