如何使这段代码不循环遍历所有工作表?

时间:2014-08-03 14:05:29

标签: excel vba

Sub SalesPersonTotal()
    Dim employee As String, total As Long, Sheet As Worksheet, i As Long

    total = 0
    employee = InputBox("Enter the employee name (case sensitive)")

    For Each Sheet In Worksheets

        For i = 6 To 35

            If Sheet.Cells(i, 5).Value = employee Then
                total = total + Sheet.Cells(i, 10).Value
            End If

        Next i
    Next Sheet

    MsgBox "Total sales of " & employee & " is " & total

End Sub

1 个答案:

答案 0 :(得分:2)

避免在所有床单上循环 删除

For Each Sheet in Worksheets

明确提及工作表名称

Sheets("YourSheetName").Cells(i, 5).Value   

取代这类代码

Sheet.Cells(i, 5).Value