2016-06-21 85 views
0

我收到“溢出”错误,但小组按预期运行。我忽略了哪些错误?提前致谢。什么是调试溢出错误?

Sub Bill_Detail_Exp_Prem_BUTTON1_() 

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 
+0

我假设有一个机会,'(wb1.Sheets( “详细信息”)细胞(我,16)) '可能是零 –

回答

1

我会修改IF逻辑有点:

For i = 7 To LastRow 
    If wb1.Sheets("Detail").Cells(i, 16) = 0 Then 
     If wb1.Sheets("Detail").Cells(i, 15) <> 0 Then 
      wb1.Sheets("Detail").Cells(i, 1) = ((wb1.Sheets("Detail").Cells(i, 15))) 
     End If 
    Else ' it means wb1.Sheets("Detail").Cells(i, 16) <> 0 , so no division by 0 and no Overflow Error 
     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 
+0

非常感谢。你们都摇滚。 –

0

很难看到有什么问题没有得到数据的视图,但你可以尝试以下方法:更换

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

LastRow = wb1.Sheets("Detail").range("C1").End(xlDown).Row + 1

而且请注意,您可以通过在代码中放置断点(F9)来调试VBA。

1

您已测试单元格(i,16)不为零,但只在单元格(i,15)<> 0的情况下进行测试。您可能需要添加另一个测试,ElseIf在继续执行最终条件之前测试Cells(i,16)为零。溢出与#DIV/0相同。