VBA loop

Deuce Bucksman

Board Regular
Joined
Jan 17, 2011
Messages
102
Hello, gurus.

This loop runs indefinitely. Any advice on how to stop it. This code works but it just goes on forever.


Sub macrotest()
Do Until IsEmpty(Cells)
For i = 2 To 6
Cells(4, i).GoalSeek goal:=Cells(6, i), changingcell:=Cells(3, i)
Next i
Loop
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi,
This is not tested, but if all else is working as desired, try just removing the lines:
Code:
Do Until IsEmpty(Cells)
[COLOR=Blue]and[/COLOR]
Loop
Does that help?
 
Upvote 0
I'm not sure how to incorporate that change. Do I delete the For next (i)? Sorry, I appreciate your assistance but want to make sure I understand.
 
Upvote 0
Oh, no it's more simple than that.
Just go into your code and (either) delete just those two lines (leaving what's between them in place) or you can put an apostrophe in front of those two lines, which will convert them from lines of (live) code to being just comments.
Your code should look like (either) this:
Code:
Sub macrotest()
[COLOR=Green]'Do Until IsEmpty(Cells)[/COLOR]
For i = 2 To 6
    Cells(4, i).GoalSeek goal:=Cells(6, i), changingcell:=Cells(3, i)
Next i
[COLOR=Green]'Loop[/COLOR]
End Sub
OR... this:
Code:
Sub macrotest()
For i = 2 To 6
    Cells(4, i).GoalSeek goal:=Cells(6, i), changingcell:=Cells(3, i)
Next i
End Sub

Either one will do what I'm suggesting.

That help?
 
Upvote 0
Try

Code:
Sub macrotest()
    Dim i As Long
    On Error Resume Next
    For i = 2 To 6
        Cells(4, i).GoalSeek goal:=Cells(6, i), changingcell:=Cells(3, i)
    Next i
End Sub

Does it help?

Biz
 
Upvote 0
Thanks, HalfAce and Biz. All codes work perfectly. Is a loop not possible with goal seek or is just not neccessary. I had to write some code today for a model an associate of mine is building, and he kept having to change the number of columns that he was trying to goal seek so I was looking for an easier way. Just curious. You both tackled the issue and the code works perfect! Thanks, again!
 
Upvote 0
Thanks, HalfAce and Biz. All codes work perfectly. Is a loop not possible with goal seek or is just not neccessary. I had to write some code today for a model an associate of mine is building, and he kept having to change the number of columns that he was trying to goal seek so I was looking for an easier way. Just curious. You both tackled the issue and the code works perfect! Thanks, again!


Why not created Range Name?

1) SetCells
2) Tovalue
3) ChangingCells

This approach may work but not tested.


Biz
 
Upvote 0
Is a loop not possible with goal seek or is just not necessary.
Yes, it's possible. The goalseek is currently being done within a loop in your existing code. (For i = 2 to 6... Next i)
The original code was running your existing loop within another loop (Do Until IsEmpty(Cells) ... Loop) that was saying "keep looping until all cells in the sheet are blank"
Of course all cells are not going to become blank. (hence the never ending loop.)

Does that make sense?
 
Upvote 0
Aw, I now see where I went wrong. I was trying to make it so the loop would keep looping across until it encountered a cell with no content, but I can see how that would go south with another loop. Really appreciate your help. You saved me (and my counterpart) countless hours of monotonous goal-seeking.
 
Upvote 0

Forum statistics

Threads
1,207,260
Messages
6,077,351
Members
446,279
Latest member
hoangquan2310

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top