通过递增值

时间:2017-07-27 14:54:21

标签: excel-vba vba excel

我试图将列“C”设置为等于范围。我希望范围为0到MaxValue,增量为0.1。所以它就像0,0.1,0.2,0.3 ...... MaxValue。当我使用这个代码时,它只给我一个整列的值。怎么会这样呢?提前致谢!

For p = 1 To MaxValue / 0.1
    For g = 0 To MaxValue
        ws1.Cells(p + 9, "C") = g + 0.1
    Next g
Next p

3 个答案:

答案 0 :(得分:1)

这样做可以在不修改代码的情况下完成。

g = 0
For p = 1 To MaxValue / 0.1

    ws1.Cells(p + 9, "C") = g
    g = g + 0.1

Next p

答案 1 :(得分:1)

这是一个简单的While循环来完成你正在寻找的东西。只需将MaxValue更改为您想要的

即可
Sub test()
    Dim MaxValue As Double
    Dim p, counter As Double
    p = 0
    counter = 1
    MaxValue = 10

    While p < MaxValue
        Sheet1.Cells(counter, 3) = p
        p = p + 0.1
        counter = counter + 1
    Wend
End Sub

答案 2 :(得分:0)

Sub test()
Dim intOffsetCounter As Integer
Dim dblMaxValue As Double
Dim dblCurrentValue As Double

dblMaxValue = 10
dblCurrentValue = 0
intOffsetCounter = 1
Do Until dblCurrentValue = dblMaxValue
    If dblCurrentValue > dblMaxValue Then Exit Do
    Sheets(1).Cells(intOffsetCounter, 3).Value = dblCurrentValue
    dblCurrentValue = dblCurrentValue + 0.1
    intOffsetCounter = intOffsetCounter + 1
Loop

End Sub

您可以通过将intOffsetCounter更改为您想要开始的任何行来更改范围的开始位置。