Inset For/As long commands

chrgrose

Board Regular
Joined
Sep 11, 2009
Messages
80
I have the code:

Code:
Option Explicit
Sub runner()
Dim a As Long
For a = 11 To 1515 Step 1
  Do Until Range(Cells(2, 1), Cells(2, 1)) > a
    Range(Cells(2, 1), Cells(2, 1)).CalculateRowMajorOrder
    Range(Cells(13, 392), Cells(13, 392)).CalculateRowMajorOrder
    Range(Cells(a, 1), Cells(a + 150, 1154)).CalculateRowMajorOrder
  Loop
Next a
End Sub

What I want to know is how to use multiple For/As long tasks set inside each other a certain way, and describe when they should occur. Ie, i want the above task to repeat ten times times, and after each time to do a different task. I would think you could write it like this but it doesn't work:

Code:
Option Explicit
Sub runner()
Dim a As Long
For b = 1 To 10 Step 1

For a = 11 To 1515 Step 1
  Do Until Range(Cells(2, 1), Cells(2, 1)) > a
    Range(Cells(2, 1), Cells(2, 1)).CalculateRowMajorOrder
    Range(Cells(13, 392), Cells(13, 392)).CalculateRowMajorOrder
    Range(Cells(a, 1), Cells(a + 150, 1154)).CalculateRowMajorOrder
  Loop
Next a

Range(Cells(5, 1), Cells(5, 1)).CalculateRowMajorOrder
Next b
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Yes, but this does not mean your code will have any effect.

If Range(Cells(2, 1), Cells(2, 1)) > a evaluates to False then no code inside that loop will execute, since the Do Loop will be skipped.

This line has the same range hard coded (I guess you could intend to recalculate it on each pass, but normally in a loop you have a dynamic element that's changing each pass):
Range(Cells(5, 1), Cells(5, 1)).CalculateRowMajorOrder

It's hard to say. I'd doublecheck your code logic. Use F8 to step through the code and debug it carefully. Usually you can answer your own questions if you step through your code and watch it execute line by line.
http://krgreenlee.blogspot.com/2006/04/programming-excel-vba-debugging-for.html

(To be honest, I've never seen CalculateRowMajorOrder before ... no idea what that is ... I assume it's an Excel 2007 or Excel 2010 thing).
 
Last edited:
Upvote 0
Yes, but this does not mean your code will have any effect.

If Range(Cells(2, 1), Cells(2, 1)) > a evaluates to False then no code inside that loop will execute, since the Do Loop will be skipped, and there are no other executable lines in the For Loops.

This line has the same range hard coded (I guess you could intend to recalculate it on each pass, but normally in a loop you have a dynamic element that's changing each pass):
Range(Cells(5, 1), Cells(5, 1)).CalculateRowMajorOrder

Hard to say. I'd doublecheck your code logic. Use F8 to step through the code and debug it carefully:
http://krgreenlee.blogspot.com/2006/04/programming-excel-vba-debugging-for.html

(To be honest, I've never seen CalculateRowMajorOrder before ... no idea what that is ... I assume it's an Excel 2007 or Excel 2010 thing).

Thanks. I just want the code that I already have to be performed in a loop, so we have a loop within a loop. Due to the way I have the native code set up, successive loops will work if I add another task setting the value in a cell A1=0, calculate once, and then set it back to 1. I just don't know how to have the VBA execute my code in a loop.
 
Upvote 0
Try the debugging tips in the link I gave. You'll probably see what's wrong when you are stepping through your code. There's nothing about loops within loops that VBA can't handle, but these can be sources of subtle errors on the part of the programmer (i.e., you or me, depending on who's writing the code).
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,854
Members
452,948
Latest member
UsmanAli786

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