我希望将列A从0开始填充到范围(“b1”)中设置的值,增量为0.1。我有下面的代码,但是当我运行它时,它不会停止在我设置的范围(“b1”)上的值。帮助
Dim x As Double
Do
x = x + 0.1
Range("A" & Rows.count).End(xlUp).Offset(1).Value = x
Loop Until x = Range("b1").Value
答案 0 :(得分:0)
尝试在比较期间转换数字。
Private Sub this()
Dim i As Long, y As Double, x As Double
x = CDbl(ThisWorkbook.Sheets("Sheet1").Range("b1").Value)
Debug.Print ; x
y = 0
i = 1
For i = 1 To 999
ThisWorkbook.Sheets("Sheet1").Range("a" & i).Value = y
y = y + 0.01
Debug.Print ; y
If CLng(y) = CLng(x) Then
Exit For
End If
Next i
End Sub
OR
Private Sub this()
Dim i As Long, y As Double, x As Double
x = CDbl(ThisWorkbook.Sheets("Sheet1").Range("b1").Value)
Debug.Print ; x
y = 0
i = 1
For i = 1 To 999
ThisWorkbook.Sheets("Sheet1").Range("a" & i).Value = y
y = y + 0.01
Debug.Print ; y
If CStr(y) = CStr(x) Then
Exit For
End If
Next i
End Sub
答案 1 :(得分:0)
对于EQUALS(长十进制)数字,使用as double可能会有问题。如果您将循环更改为Loop Until x >= Range("b1").Value
,则应获得理想的结果。
答案 2 :(得分:0)
你可以"转置"将整个问题转换为整数,然后将它们除以10得到小数
Option Explicit
Sub main()
Dim iLoop As Long
For iLoop = 1 To Range("b1").Value * 10 + 1
Range("A" & iLoop).Value = (iLoop - 1) * 0.1
Next
End Sub