PDA

View Full Version : loop in VB


Agtuary
06-10-2002, 09:57 AM
I have a simple macro in Excel. It cuts and pastes some information, then performs a little data manipulation. I have included a loop so it does it XX times. My loop is working but I cannot get the macro to change the value in a specific cell. This value controls what information is in the cells being moved and manipulated.

The code is essentially:



L = Range("L3")
For i = L To 1 Step -1
Range("K3").Select
ActiveCell.FormulaR1C1 = L

{perform some actions}
next i
End

It runs the loop L times but only sets K3 = L the first time. How do I get it to change K3 each time?

Agtuary
06-10-2002, 09:59 AM
One other question:

I have this macro saved in my personal workbook, how do I save it in another file?

Ben Kenobi
06-10-2002, 10:05 AM
If L is a number and not a formula, use the Value property, not the Formula property. (That's a guess on my part.)

To save it in another workbook, select the workbook, Insert Module, and copy/paste the code into the new module.

Pi Man
06-10-2002, 10:12 AM
if you expect L3 to change after each loop, and want L to reflect that value, then the first line of your code should be inside the loop. Since your loop depends on the value of L, i think it should be something like this:

L = Range("L3")
For i = L To 1 Step -1
L = Range("L3")
Range("K3").Select
if L is a value, then I think the assignment of L should look like this:

L = Range("L3").value

hope this helps...

Gandalf
06-10-2002, 10:12 AM
I agree with Ben about Range("K3").value = L instead of Range("k3").formulaR1c1 = L.

Don't you also need to start with L = Range("L3").value? Or is that property assumed by default?

Finally, unless K3 is being changed by those "(perform some actions)", how do you know whether it is being set once or multiple times?

Agtuary
06-10-2002, 10:42 AM
I figured out the problem. I wanted K3 = i, not L.

Thanks for helping.