简体   繁体   中英

Looping with varying range (Macros)

I am trying to wirte a macros that will interpolate between two points.

This is what i have so far:

   Sub Withoutloop()

   If Range("E2") >= Range("A2") And Range("E2") <= Range("A3") Then

Range("E3").FormulaR1C1 = "=(R[-1]C[0])/((R[0]C[-4]-R[-1]C[-4])/(R[0]C[-3]-R[-1]C[-3]))"

ElseIf Range("E2") >= Range("A3") And Range("E2") <= Range("A4") Then

Range("E3").FormulaR1C1 = "=(R[-1]C[0])/((R[1]C[-4]-R[0]C[-4])/(R[1]C[-3]-R[0]C[-3]))"

ElseIf Range("E2") > Range("A4") Then

Range("E3") = "Fail"

End If

End Sub

As you can see, the first if part is seeing if E2 is between A2 and A3, but if E2 isnt between A2 and A3 it then moves on to see if E2 is between A3 and A4 etc, I would like a code that automatically carries this process on.

Secondly, in the section if i have found the two cells E2 is between in then calculates the x value for me.

Range("E3").FormulaR1C1 = "=(R[-1]C[0])/((R[0]C[-4]-R[-1]C[-4])/(R[0]C[-3]-R[-1]C[-3]))"

in each loop i would like the cells to move down one.. shown here:

Range("E3").FormulaR1C1 = "=(R[-1]C[0])/((R1C[-4]-R[0]C[-4])/(R1C[-3]-R[0]C[-3]))"

Is there a way of incorporating this into a loop.

Excel

Thank You!

You could start with a case select statement. you'll need to supply your formula. If you need the cells and ranges to be dynamic, you can replace the cell row reference with a variable. Like I as in For I = 2 to last row Range("E" & I) or Range("A" & i)

Select Case Range("E2").Value
 Case Is >= Range("A2").Value
    If Range("E2").Value < Range("A3").Value Then
    Range("E3").FormulaR1C1 = "your formula"
    End If
 Case Is >= Range("A3").Value
    If Range("E2").Value < Range("A4").Value Then
    Range("E3").FormulaR1C1 = "your formula"
    End If
 Case Is >= Range("A4").Value
    If Range("E2").Value < Range("A5").Value Then
    Range("E3").FormulaR1C1 = "your formula"
    End If
 Case Is >= Range("A5").Value
    If Range("E2").Value < Range("A6").Value Then
    Range("E3").FormulaR1C1 = "your formula"
    End If

Else
    Range("E3").Value = "Fail"
End Select

A quick walk-through of looping using a For loop (not For Each) might help... my recommendation is go get used to using Cells(), which makes it pretty easy to keep track.

Will show the basic loop going down rows (assumes you have output in every other row (3, 5, 7, etc.)) for: If Range("E2") >= Range("A2") And Range("E2") <= Range("A3") Then

Dim i as Long
For i = 2 to 10 Step 2 
    If Cells(i,"E").Value >= Cells(i, "A").Value  AND Cells(i, "E").Value <= Cells(i+1, "A") Then
        Cells(i+1, "E").Formula = "insert formula"
    End If
Next i

If this were to go along columns, you would change where your variable is (and in this case, would probably want to use numbers, eg A = 1, E = 5 (shows up like Cells(1, 5) = Cells(1, "E") = Range("E1")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM