Home » excel » excel – Loop with a 0.25 step value

excel – Loop with a 0.25 step value

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a data reliant on a value based in one cell. I need to gather the data by changing this value by a 0.25 step value.

I was trying to do it separately in VBA, however it is very tedious.

Sub total_usage2()
    Sheets("1_GENERAL").Activate
    Range("A2").Value = 49
    Sheets("4.MINUTES").Activate
    Range("BL371").Activate
    Range("BL371").Copy
    Sheets("9").Activate
    Range("A2").PasteSpecial xlPasteValues
    Sheets("1_GENERAL").Activate
    Range("A2").Value = 49.25
    Sheets("4.MINUTES").Activate
    Range("BL371").Activate
    Range("BL371").Copy
    Sheets("9").Activate
    Range("A3").PasteSpecial xlPasteValues
    Sheets("1_GENERAL").Activate
    Range("A2").Value = 49.5
    Sheets("4.MINUTES").Activate
    Range("BL371").Activate
    Range("BL371").Copy
    Sheets("9").Activate
    Range("A4").PasteSpecial xlPasteValues
End sub

I prepared the loop for this occasion

Sub calculation()
    Dim i As Integer
    For i = 0 To 90 Step 0.25
    Sheets("1_GENERAL").Range("a2") = CStr(i)
    Sheets("9").Range("A" + CStr(i + ((i - 1) * 1))).Rows(3).PasteSpecial xlPasteValues
    Next i
End Sub

I want to change the value (see in the picture) for every 0.25 step and paste it in another worksheet in columns one by one.

enter image description here

How to&Answers:

If you’re stepping by 0.25, you can’t use an integer – use a single. Now you can perform the loop:

Sub foo()
    Dim i As Single
    With Worksheets("9")
        .UsedRange.Clear
        .Range("A1").Value = "Input"
        .Range("B1").Value = "Output"
        For i = 0 To 90 Step 0.25
            Worksheets("1_GENERAL").Range("A2").Value = i
            With .Cells(Rows.Count, 1).End(xlUp)
                .Offset(1, 0).Value = i
                .Offset(1, 1).Value = Worksheets("4.MINUTES").Range("BL371").Value
            End With
        Next i
    End With
End Sub