循环值在一个单元格内最多可保留小数点后三位

时间:2018-11-12 13:42:33

标签: excel vba excel-vba

Dim j As Long
 Dim mycell As Range

   For j = 0 To 24

      Range("C12").Value = j

      Range("C93").Formula = "=Round(Sin(C12*pi()/180)*(C84+C88),3)"
      Range("C94").Formula = "=Round((Sin(C12*pi()/180)*(C84+C88))/1000,2)"

      Range("C96").Formula = "=Round(-1*(C80-C93),3)"
      Range("C98").Formula = "=Round((-1*(C80-C93))/1000,2)"

      If Range("C94").Value = Range("C82").Value Then
      MsgBox "The Maximum value of Theta is " & Range("C12").Value & Chr(10)

      Exit For
      End If

    Next

我遵循的代码将j从0循环到24。示例:0,1,2,.... 24
但是我如何从0.001到0.999,然后从1.101 .....直到24循环。

如何循环到所需的小数位?

3 个答案:

答案 0 :(得分:1)

也许是这样的:

Sub Test()

    Dim j As Long, k As Long

    For k = 0 To 24
        For j = 1 To 999
            Debug.Print k + (j / 1000)
        Next j
    Next k

End Sub

答案 1 :(得分:1)

您可以使用类似的内容创建内部循环

For j = 0 To 24
   Dim d As Double
   For d = 0 To 0.999 Step 0.001
       Range("C12").Value = j + d 
       ...
   Next d
Next j

答案 2 :(得分:0)

这里的想法是保持循环简单,并在循环内排除不需要的值:

Public Sub Test()
   Dim j As Double

   For j = 0.001 To 24 Step 0.001
      If j < 1 Or j > 1.1 Then
         Debug.Print Format(j, "0.000")
      End If
   Next
End Sub