Need loop examples

Ronanm

Board Regular
Joined
Nov 13, 2010
Messages
107
Hi

I am currently trying to get my head around using Excel VBA, and have made good progress over the past year.

However, I still struggle tremendously with Loops within Loops, whether they are For Each, Do Until, Do While etc...

I have read various books, but still struggle.

Does anyone now of, or have Examples, that are heavily commented, which I could use as an aid in learning.

Many thanks

Ronan
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Trying to learn loops through just reading code would be incredibly hard. What I would suggest is finding (or creating) a scenario where you believe a loop would be effecient, then posting back in this topic so I, or another user, can create some code that will explain each step. That way you have an idea of what you want, you have data to actually look at, and you can understand why and how the macro loops.
 
Upvote 0
I have found that unless you work with real world examples, examples from books don't stick.

Food for thought:
For/Next - Good for starting at one number and counting to the end.

For A = 1 to 100
Range("A" & A) = A
Next

For Each/Next - Works with collections, whether that be cells, worksheets, shapes, etc.

Set Rng = Range("A1:A" & LastRow)
For Each aCell in Rng
'Multiple by 2 and put in next column over.
aCell.Offset( ,1).Value = aCell.value * 2

Do/Loops run until a certain condtion is met. They tend to take a little more work to make sure the conditions are met, else you get a 'til loop'. (Till the cows come home.)

The important thing about Until/While is whether they are at the top or bottom of the loop. Depending on the logic, one will never go into the loop and the other will go through the loop at least one time.
 
Upvote 0
Here's a couple I keep handy.

' For Loop
Rim R
R = 3
For G = 1 to R
do code
Next G


'Do While Loop
Do While Q >= 1
Sheets.Add
ActiveSheet.Select
ActiveSheet.Name = "New" & Q
Q = Q + 1
Loop
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,094
Latest member
mystic19

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