阻止Sub运行它需要什么似乎运行了很长时间?

时间:2016-06-14 18:15:45

标签: excel vba

我收到了#34;溢出"信息。代码按预期工作,但运行了很长时间。任何建议。

Sub Bill_Detail_Exp_Prem_Var()

    Dim LastRow As Long
    Dim wb1 As Workbook
    Dim i As Long

    Set wb1 = Workbooks("macro all client v.01.xlsm")

    LastRow = wb1.Sheets("Detail").range("C:C").Find("", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    For i = 7 To LastRow
        If wb1.Sheets("Detail").Cells(i, 15) <> 0 And wb1.Sheets("Detail").Cells(i, 16) = 0 Then
            wb1.Sheets("Detail").Cells(i, 1) = ((wb1.Sheets("Detail").Cells(i, 15)))
        Else
            wb1.Sheets("Detail").Cells(i, 1) = ((wb1.Sheets("Detail").Cells(i, 17)) * (wb1.Sheets("Detail").Cells(i, 15))) / (wb1.Sheets("Detail").Cells(i, 16))
        End If

    Next i
End Sub

2 个答案:

答案 0 :(得分:1)

您的支票

If wb1.Sheets("Detail").Cells(i, 15) <> 0 And wb1.Sheets("Detail").Cells(i, 16) = 0 Then

不检查这两者是否等于零。执行Overflow时,错误0/0确实会发生。

Dim LastRow As Long
Dim wb1 As Workbook
Dim i As Long

Set wb1 = Workbooks("macro all client v.01.xlsm")

LastRow = wb1.Sheets("Detail").Range("C:C").Find("", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

For i = 7 To LastRow
    Dim destinationCell As Range
    Set destinationCell = wb1.Sheets("Detail").Cells(i, 1)
    Dim parameterFifteen, parameterSixteen, parameterSeventeen

    parameterFifteen = wb1.Sheets("Detail").Cells(i, 15)
    parameterSixteen = wb1.Sheets("Detail").Cells(i, 16)
    parameterSeventeen = wb1.Sheets("Detail").Cells(i, 17)

    If parameterFifteen <> 0 And parameterSixteen = 0 Then
        destinationCell = parameterFifteen
    ElseIf parameterSixteen <> 0 Then
        destinationCell = parameterSeventeen * parameterFifteen / parameterSixteen
    Else
        'something else ?
    End If

Next i

答案 1 :(得分:1)

这是有效的吗?

LastRow = wb1.Sheets("Detail").range("C:C").Find("", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

不应该

LastRow = wb1.Sheets("Detail").range("C:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row