没有For,无法弄清楚VBA错误

时间:2017-08-08 12:05:39

标签: vba excel-vba excel

我正在研究一组更大的代码,当测试这一部分时,我得到错误Next not For。我已经看了,每个For有一个Next,每个If有End If If和Every While有一个Wend。

你能帮我搞清楚吗?

提前致谢!

Option Explicit

Sub trueTotal()

Dim ws As Worksheet
Set ws = Worksheets("Sheet1") 'be more explicit than 'ActiveSheet
Dim sPart As String
Dim SearchRange As Range
Dim FindRow As Range
Dim ReturnRowNumber As Integer
Dim currPickTotal As Integer
Dim totalPick As Integer
Dim scanRow As Integer
Dim scanQty As Integer
Dim cell As Range
Dim rng As Range

Set FindRow = Nothing

'Compare the range and loop until equal
While ws.Range("E6:E30") <> ws.Range("F6:F30")
    Set rng = ws.Range("E6:F30")
    For Each cell In rng
        If Not cell Is Empty Then
            ws.Range(rng).Interior.ColorIndex = 4
    Next cell
        While FindRow Is Nothing
            sPart = InputBox("Scan the first part number", "Part Number")  'Get Part Number
            'Set the search range to get the cell row for the part number.
            Set SearchRange = ws.Range("A6", ws.Range("A30").End(xlUp))
            Set FindRow = SearchRange.Find(sPart, LookIn:=xlValues, LookAt:=xlWhole)
            If Not FindRow Is Nothing Then ReturnRowNumber = FindRow.Row
        Wend
        scanRow = ReturnRowNumber
        'Error checking to ensure scanned amount is not greater than required amount
        If totalPick <= ws.Range("E" & scanRow) Then
            scanQty = InputBox("Scan the Quantity", "Quantity")    'Get Quantity
            currPickTotal = ws.Range("F" & scanRow).Value
            totalPick = currPickTotal + scanQty
            Range("F" & scanRow).Value = totalPick
            Set FindRow = Nothing
            If Range("E" & scanRow).Value = Range("F" & scanRow).Value Then
                Range("E" & cell.Row).Interior.ColorIndex = 3
                Range("F" & cell.Row).Interior.ColorIndex = 3
            End If
        Else
            scanQty = InputBox("Scan exceeds the required Quantity, please scan a lower quantity.", "Quantity")    'Get Quantity
            currPickTotal = ws.Range("F" & scanRow).Value
            currPickTotal = currPickTotal - scanQty 'Subtract last scanQty
            totalPick = currPickTotal + scanQty
            Range("F" & scanRow).Value = totalPick
            Set FindRow = Nothing
            If Range("E" & scanRow).Value = Range("F" & scanRow).Value Then
                Range("E" & cell.Row).Interior.ColorIndex = 3
                Range("F" & cell.Row).Interior.ColorIndex = 3
            End If
        End If
Wend
End Sub

2 个答案:

答案 0 :(得分:2)

你错过了End If ...

For Each cell In Rng
    If Not cell Is Empty Then
        ws.Range(Rng).Interior.ColorIndex = 4
    End If
Next cell

希望这有帮助!

答案 1 :(得分:0)

我建议关注;

...
While Not equal_E6_E30_to_F6_F30(ws)
...

Function equal_E6_E30_to_F6_F30(ws As Worksheet)
Dim i As Integer
equal_E6_E30_to_F6_F30 = True
For i = 6 To 30
    If ws.Range("E" + Trim(Str(i))).Value <> ws.Range("F" + Trim(Str(i))).Value Then
        equal_E6_E30_to_F6_F30 = False
        Exit For
    End If
Next
End Function

我已经测试过了。