VBA For Loop - A Complete Guide

Ғылым және технология

👉 Ready to master VBA?
- Check out my full courses: courses.excelmacromastery.com/
- Subscribe to the channel here: bit.ly/36hpTCY
-Want to download the source code for this video? Go here bit.ly/3CKyRIA
(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

Пікірлер: 67

  • @Excelmacromastery
    @Excelmacromastery3 жыл бұрын

    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.

  • @vijaysahal4556

    @vijaysahal4556

    3 жыл бұрын

    Hi I wana to say ur for loop class was very amazing bcz your teaching way is very unique.. nd clear so m loving it tq 👍🏻👍🏻👍🏻

  • @andyg1881

    @andyg1881

    3 жыл бұрын

    I can't download the source code with 20+ For Loop examples, The link takes me to download a PDF file named "VBA Vitals cheat sheet". Thanks.

  • @wayneedmondson1065
    @wayneedmondson10653 жыл бұрын

    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!!

  • @frikduplessis8849
    @frikduplessis88493 жыл бұрын

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

  • @vipulDJhaveri
    @vipulDJhaveri3 жыл бұрын

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

  • @SimpleExcelVBA
    @SimpleExcelVBA3 жыл бұрын

    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.

  • @rskh3763
    @rskh37635 ай бұрын

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

  • @AlexB_o7
    @AlexB_o73 жыл бұрын

    Please continue posting this kind of guides!

  • 3 жыл бұрын

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

  • @free3690
    @free36903 жыл бұрын

    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.

  • @michaeljarosz4062
    @michaeljarosz40622 ай бұрын

    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]

  • @Rajan-kk8nl
    @Rajan-kk8nl3 ай бұрын

    Very very intersting class, thank you so much sir.

  • @sasavienne
    @sasavienne2 жыл бұрын

    Thank you so much Paul.

  • @haribhaskar72
    @haribhaskar723 жыл бұрын

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

  • @prasanthjayaraman8087
    @prasanthjayaraman80873 жыл бұрын

    Sir, Very nice and well explained. Thanks a lot .....

  • @ecarlosbc
    @ecarlosbc3 жыл бұрын

    Great work!

  • @jonnypeace2810
    @jonnypeace28103 жыл бұрын

    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

  • @amarmajeed1733
    @amarmajeed17332 жыл бұрын

    Thankyou for the facile explanation.

  • @tomharrington1453
    @tomharrington14533 жыл бұрын

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

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    Glad you like it Tom

  • @RogerFPrida
    @RogerFPrida3 жыл бұрын

    Masterpiece!

  • @thearchibaldtuttle
    @thearchibaldtuttle3 жыл бұрын

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

  • @bcostell69
    @bcostell693 жыл бұрын

    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

  • @johnwayne8059
    @johnwayne80593 жыл бұрын

    Nice, as always!👍👍👍

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    Thanks John.

  • @iswadi-edukasidanhobbi7347
    @iswadi-edukasidanhobbi73478 ай бұрын

    Thank you

  • @vijaysahal4556
    @vijaysahal45563 жыл бұрын

    Very useful nd very clear your teaching way tq sir 👍🏻👍🏻👍🏻👍🏻

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    Thanks Vijay

  • @Joehtoo
    @Joehtoo2 жыл бұрын

    Great video! Thanks

  • @Excelmacromastery

    @Excelmacromastery

    2 жыл бұрын

    You're welcome!

  • @bagnon
    @bagnon3 жыл бұрын

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

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    The upside is that it's neater to write. The downside is that it's not flexible if you need a different order or just some of the cells.

  • @alializadeh8195
    @alializadeh8195 Жыл бұрын

    Thank ya

  • @janezklun
    @janezklun3 жыл бұрын

    Good one, thank you, Cheers

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    Glad you enjoyed it

  • @JoaoNeto-wz1sb
    @JoaoNeto-wz1sb2 жыл бұрын

    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.

  • @CapriconSaturn
    @CapriconSaturn3 жыл бұрын

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

  • @BassamThebian
    @BassamThebian6 ай бұрын

    Hi, thank you, that was really great and professional, I have a problem though. am writing a code with (for) (for r = 27 to 32) and I already added if isempty function. but it's always taking the whole range and not stopping on empty cells. even though that same code worked on other application but not this one. is their any reason?? Hint: my data is not in separate sheet, in the same sheet I have other data, would that affect? I will very much appreciate if you can solve this problem for me. thank you.

  • @edwingonzalez2749
    @edwingonzalez2749 Жыл бұрын

    Hi, in the section "How the For Loop works" you have to define the worksheet that "shNumbers" is. My code was breaking until I figured I had define which worksheet would be "shNumbers". Hope this helps someone in the future.

  • @Jaiemimoyu
    @Jaiemimoyu5 ай бұрын

    Nice video, would you mind teach me more? Why "For i = 1 to 3" is 3 times looping only, but the final i is 4? I don't understand the logic here.

  • @rohithnlp485
    @rohithnlp4852 жыл бұрын

    i love you paul from INDIA

  • @ananthuh0953
    @ananthuh09532 жыл бұрын

    Is it possible to give the sheetname, ranges, copy, destination sheet, destination range,and using lopping copy and paste based on the inputs given

  • @jk.tal22
    @jk.tal222 жыл бұрын

    Hey paul, how can you enter a new line if a condition is met?

  • @luismateo6690
    @luismateo66903 жыл бұрын

    Very nice video

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    Thanks

  • @1612ganesh
    @1612ganesh3 жыл бұрын

    Hi sir, please make a video on Userform.

  • @vijaysahal4556
    @vijaysahal45563 жыл бұрын

    Nice sir 👍🏻👍🏻👍🏻👍🏻

  • @1578291978
    @15782919789 ай бұрын

    Hi, In your Number Input box example, I want to use a cell value instead of user value how to do that... Plz help me out

  • @nextgenerationforchrist320
    @nextgenerationforchrist320 Жыл бұрын

    Hi, how to make a loop in criteria of an autofilter in a column

  • @Tattysnuc
    @Tattysnuc3 жыл бұрын

    great stuff. You stated: Make sure to download the source code with 20+ For Loop Examples from the description where are they please?

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    Beside "Download the Source Code"

  • @andyg1881

    @andyg1881

    3 жыл бұрын

    @@Excelmacromastery , No, the Link take you to download a PDF file "VBA Vitals Cheat sheet"

  • @rukkyneel2174
    @rukkyneel21742 жыл бұрын

    hi Sir, I am stuck somehwere in excel. I hope you can help me. I am generating a random number using vba command (randbetwee,1,6)...and i want to add the result each time when i clcik the button.. on click 1: random number 5, Store the value 5 on click 2: random number 2, store the value 7 on click 3: random number 1, store the value 8..and so on...can you please help me..many thanks in advnace.

  • @homejonny9326
    @homejonny93263 жыл бұрын

    Please, which is this very nice mono font? Consolas?

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    Yes

  • @SamehRSameh
    @SamehRSameh7 ай бұрын

    When i write formula in vba , and when argument need write number, i make loop to sign number,,,, but code not working?? Why

  • @alexeiofp
    @alexeiofp3 жыл бұрын

    HI master, I'm from Santiago Chile, how can a buy your TheExcelVBAHandbook in equal parts? I can buy pdf1 in 40usd and so on?

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    Hi Alexis, It can only be bought as one single course.

  • @alexeiofp

    @alexeiofp

    3 жыл бұрын

    @@Excelmacromastery ok.

  • @DSureshKumar
    @DSureshKumar3 жыл бұрын

    Vba is an excellent subject then why Microsoft company not encouraging such a nice language.. Plz advice. I will miss vba in my future.. It's eazy as far as what I knew

  • @SKarea51
    @SKarea513 жыл бұрын

    I thunk in the first example of nested loop you wanted to use... Cells(i, j).. Instead of Cells(i, 1)..

  • @Excelmacromastery

    @Excelmacromastery

    3 жыл бұрын

    Yes, I was focusing on the i and j values. Cell should be i,j for the nested loop.

  • @brandonly27
    @brandonly272 жыл бұрын

    Do you realize how much money people can make with the right background using these skills? SCADA uses these principals all the time. Especially in the oil and gas industry. I am just barely scratching the surface of VBA in excel and I have already changed the company.

  • @geoffreyjones3832
    @geoffreyjones38323 жыл бұрын

    I have no idea what you're talking about.

  • @gerardvaneggermond2067
    @gerardvaneggermond2067 Жыл бұрын

    Hello sir, maybe you can help me, I made a code but it is too long now I want to reduce it, is this possible? One section has the following code; Application.ScreenUpdating = False If Sheets("Menu").Range("E2") = 1 Then Sheets("toplay1").Select Range("A1").Select5 ActiveCell.Formula2R1C1 = _ "=FILTER('S1'!R[1]C[1]:R[20]C[1],'S1'!R[1]C[1]:R[20]C[1]" "")" Range("B1").Select ActiveCell.Formula2R1C1 = _ "=FILTER('S1'!R[1]C[5]:R[20]C[5],'S1'!R[1]C[5]:R[20]C[5]" "")" This continues, at (c[ ]) each time 4 is added until 37 is reached Could you help me? Thanks in advance. d

Келесі