Vba 划分并依次计算划分的数量

Vba 划分并依次计算划分的数量,vba,function,excel,Vba,Function,Excel,因此,我试图获得连续除以两个整数N和D所需的除数,直到余数大于或等于0.000001。我不知道我错在哪里 用于N、D和Z的数据类型是错误的还是其他原因 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").Va

因此,我试图获得连续除以两个整数
N
D
所需的除数,直到余数大于或等于
0.000001
。我不知道我错在哪里

用于
N
D
Z
的数据类型是错误的还是其他原因

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
选项显式
副总理()
作为整数的Dim N
作为整数的Dim D
暗Z一样长
Dim intCount为整数
带工作表(“功能”)
N=单元格(16,“B”)。数值
D=单元格(16,“C”)。数值
如果D<1,则
MsgBox“除数小于1,输入大于1的值”
出口接头
其他的
如果结束
整数=0
当Z>=0.000001时执行
Z=N/D
intCount=intCount+1
N=Z
环
单元格(16,“D”)。值=整数计数
以
端接头

有几个问题。请参阅下面的评论

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
Sub-Ediv()

Dim N As Double'使用
Dim N As Double、D As Double、Z As Double试试。整数的性质不能包含混合数的小数部分;只有1,2,3,,等等。@Jeeped谢谢,但仍然没有更改。请提供一些例子说明B16:C16中的内容以及您期望的内容。@Jeeped示例:B16=3和C16=10,则intcount最后一个值将为6,因为将B16除以C16总共需要6个时间才能得到小于等于0.000001的结果第一次迭代-3/10=0.3第二次迭代-0.3/10=0.03第三次迭代-0.03/10=0.003。因此,直到第六次迭代-0.000003/10=0.0000003,第七次迭代的结果是它的值小于0.0000003,然后它将以intcount值退出并打印它++尼斯:)啊,你也开始使用
“@santosh-根据你的3和10样本,我得出了7。我意识到,
intCount
只有在进入另一个循环时才应该递增。请在2分钟内查看上面的内容。@Jeeped是的,这是唯一的问题,但我在打印之前将其减少value@Santosh-我注释掉了您的编辑,因为我已经添加了条件递增。如果没有通过,您的方法将导致-1而不是零。@jeeped我有一个问题,您知道如何使用vba将12小时时间转换为24小时格式时间,例如,下午1:30将是1330?