VBA For Loop - A Complete Guide

preview_player
Показать описание
👉 Ready to master VBA?
(Note: If the download page doesn't work then make sure to turn off any popup blockers)

VBA For Loop - A Complete Guide

In this video I will show you how to use the For Loop in VBA. The For Loop is an extremely powerful part of VBA programming and it is very important to understand it. Make sure to download the source code below which has 20 examples of using For Loops with ranges, arrays, the dictionary and more.

#VBAFORLOOP #VBALOOP #EXCELVBAFOR

Useful VBA Shortcut Keys
========================

Debugging:
Compile the code: Alt + D + C OR Alt + D + Enter
Run the code from the current sub: F5
Step into the code line by line: F8
Add a breakpoint to pause the code: F9(or click left margin)

Windows:
View the Immediate Window: Ctrl + G
View the Watch Window: Alt + V + H
View the Properties Window: F4
Switch between Excel and the VBA Editor: Alt + F11
View the Project Explorer Window: Ctrl + R

Writing Code:
Search keyword under cursor: Ctrl + F3
Search the word last searched for: F3
Auto complete word: Ctrl + Space
Get the definition of the item under the cursor: Shift + F2
Go to the last cursor position: Ctrl + Shift + F2
Get the current region on a worksheet: Ctrl + Shift + 8(or Ctrl + *)
To move lines of code to the right(Indent): Tab
To move lines of code to the left(Outdent): Shift + Tab
Delete a Line: Ctrl + Y(note: this clears the clipboard)

Table of Contents:

00:00 - Introduction
00:27 - Why do we need the For Loop?
02:35 - How to Create a For Loop
03:51 - How the For Loop works
07:57 - Exiting the For Loop
08:44 - Using a For Loop to read a Collection
10:02 - Using a For Loop to read a 1D Array
11:11 - Using a For Loop to read a 2D Array
Рекомендации по теме
Комментарии
Автор

Great tutorial as always Paul 👌 clear and to the point, must say it's been a long time coming

frikduplessis
Автор

Hi Paul. Awesome lesson, as always! Nice to refresh these concepts and reinforce them in memory with practice and examples. Thanks for sharing :)) Thumbs up!!

wayneedmondson
Автор

Let me know what you think about For Loops in the comments.

Make sure to download the source code with 20+ For Loop Examples from the description.

Excelmacromastery
Автор

Brilliant - Crisp and to the point, thanks a lot Sir

vipulDJhaveri
Автор

I wish I had such teacher at the beginning. Professional video.
Anyway, I'm a fan, like to listen to You smoothly explaining those things.

SimpleExcelVBA
Автор

Please continue posting this kind of guides!

AlexB_o
Автор

I did not know For Each ran faster than For i. I always used For i because I could use the i within the loop. Thank you for the guide, very thorough.

free
Автор

Thank you, your description is very clear and easy to understand.

rskh
Автор

I'm starting with VBA... It's very useful to know how manage a For Next loop... Thanks!

Автор

Actually, using i as the loop counter is a holdover from Fortran. In Fortran, what letter a variable name begins with implicitly determines what type of number it is. Loop counters need to be an integer type. The letters i thru n were automatically integers in Fortran so the counter in a Fortran Do loop began with i, j, k, l, m or n. Most coders used them in alphabetical order so i = 1 occurred most often and it became a convention to start loops in Fortran with i. [The Do loop in Fortran is the equivalent of the For loop in VBA]

michaeljarosz
Автор

Excellent...well explained...thanks for this video...

haribhaskar
Автор

Sir, Very nice and well explained. Thanks a lot

prasanthjayaraman
Автор

I think if I used arrays, lbound and ubound more often, my code would be easier lol. Need to start using them more often 👍 Great video

jonnypeace
Автор

very clear, im trying to simulate time and have cells coloured in real time from milliseconds to seconds, each with differing intervals depending on user input, does VBA have built in time functions

bcostell
Автор

Lbound for the low variable in array loops... I always learn something. Thank you for the lesson.

tomharrington
Автор

Very very intersting class, thank you so much sir.

Rajan-kknl
Автор

Very nice! Just be aware that arrays can be zero based as well. Your I would then need to start at 0.

thearchibaldtuttle
Автор

For looping through ranges, I like the additional Dim cell as Range method where it's for each cell in rg, next cell, etc.

bagnon
Автор

Hi, thans a lot for your videos they are helping me a lot. I would like to ask abouy a doubt i have. Which aproach it is faster, a for loop or autofill? I handle with large data with 100.000 rows so the theme faster is important. And one more time, thanks a lot for your work.

JoaoNeto-wzsb
Автор

Been followed you channel for awhile, do you have a recommended list to properly start from beginning level from your video?

CapriconSaturn