do while error

JPCAPL

New Member
Joined
Jul 7, 2011
Messages
6
I can do a simple do while loop
Code:
Dim i as Integer
Do While i < 102
  i = i + 1
Loop
MsgBox "done"

When I add a cell value based on "i," I get an error
Code:
Do While i < 102
  Worksheets("Sheet1").Cells(i, 12).Value = 3
  i = i + 1
Loop
MsgBox "done"

Why, and how can I get around it?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Have you initialised i? You don't have a row 0 if not ;-)

Code:
i = 1
Do While i < 102
  Worksheets("Sheet1").Cells(i, 12).Value = 3
  i = i + 1
Loop
MsgBox "done"
 
Upvote 0
Yes, I do have my i initialized to i = 1. It is giving me "Code execution has been interrupted," and it highlights i = i + 1.
 
Upvote 0
Try placing this at the top of your routine and then try running it again:

Code:
application.EnableCancelKey = xlDisabled
 
Upvote 0
This can be done without a loop, but if you are going to use a loop, why not a For Next Loop?
Code:
Sub test()
Dim i As Integer
For i = 1 To 102
    Worksheets("Sheet1").Cells(i, 12).Value = 3
Next i
End Sub

This however will do the same thing:

Code:
Sub test2()
Range("L1:L102") = 3
End Sub
 
Upvote 0
I broke my message down to the essentials. I am actually trying to mine information from two columns of data. There are positive and negative values as a function of time. I am trying to gather all of the ranges of negative values. For each range I am going extract the Max and Min value, and the time in that range. I am close. I spent hours on this one error, so putting it behind me is a big help. I am sure there is a more elagant way to do this, but it has been a while, so I am just pounding it out with nested do's, for's, and if's.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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