2015-09-19 114 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