2015-09-19 113 views
1

所以我试图让两个整数ND分先后,直到余数是大于或等于0.000001需要分割的数量。我不知道我错在哪里。划分和计算划分数连续

数据类型是否用于NDZ错误,还是别的?

Option Explicit 
Sub Ediv() 

    Dim N As Integer 
    Dim D As Integer 
    Dim Z As Long 
    Dim intCount As Integer 

    With Sheets("Functions") 

     N = Cells(16, "B").Value 
     D = Cells(16, "C").Value 

    If D < 1 Then 
    MsgBox "Divisor is less than 1 enter value greater than 1" 
    Exit Sub 
    Else 
    End If 

    intCount = 0 


     Do While Z >= 0.000001 
      Z = N/D 
      intCount = intCount + 1 
      N = Z 
     Loop 

Cells(16, "D").Value = intCount 
End With 
End Sub 
+0

试一下'Dim N As Double,D As Double,Z as Double'。整数本质上不能包含混合数的小数部分;只有1,2,3等。 – Jeeped

+0

@Jeeped谢谢,但仍然没有改变。 –

+0

提供B16:C16中的一些示例以及您的期望。 – Jeeped

回答

2

有几个麻烦的地方。请参阅以下注释。

Sub Ediv() 

    Dim N As Double '<~~ Integers cannot be decimal numbers 
    Dim D As Double '<~~^same^
    Dim Z As Double '<~~^same^
    Dim intCount As Long '<~~ might be a long count 

    With Sheets("Functions") 

     N = .Cells(16, "B").Value '<~~ these were Cells, not .Cells so they were not explicitly children of the Functions worksheet 
     D = .Cells(16, "C").Value '<~~^ same^

     If D < 1 Then 
      MsgBox "Divisor is less than 1 enter value greater than 1" 
      Exit Sub 
     End If 

     intCount = 0 


     Z = N/D '<~~ Z was only initialized not assigned so it was zero 
     Do While Z >= 0.000001 
      Z = N/D 
      intCount = intCount - CBool(Z >= 0.000001) '<~~only increment if it will go into another loop; True is -1 in VBA. 
      N = Z 
     Loop 
     'intCount = intCount -1 'decrement by 1 if not conditionally incremented 
     .Cells(16, "D").Value = intCount '<~~ fixed parent worksheet here as well 
    End With 
End Sub 
+0

++好:)你丫也开始使用'” <~~':D –

+0

@Jeeped非常感谢你的回答。 –

+0

@santosh - 根据你的3和10的样本,我想出了7个。我意识到'intCount'只应该增加,如果它进入另一个循环。 2分钟见上面。 – Jeeped