VBA- Loops

In VBA, The For Loop is used when you can determine the number of times it will be run or number of iterations are already know in advance. For example if you want to repeat something ten or twenty times.

Syntax

For <variable> = <start value> to <end value> Step <number>

Set of lines goes here….

Next <variable>

So let’s take an example where I have to print first 5 natural numbers using For Loop

Sub Test()

Dim x As Integer

For x = 1 To 3 Step 1

MsgBox x

Next i

End Sub

The above code can be modified and could be write in short way as below-

Sub Test()

Dim x As Integer

For x = 1 To 3

MsgBox x

Next

End Sub

Please Note, This will also produce the same output as you can notice we have remove Step (As by default For Loop takes 1 step) and also remove x after Next because here we have only Single loop to execute which is understood by VBA Complier.

Example: To print first 10 Natural Numbers in Excel Sheet

Sub Test()

Dim x As Integer

For x = 1 To 10

Cells(x,1).value=x

Next

End Sub

After executing this code you will get a series of numbers from 1 to 10 in Excel Sheet from A1 to A10 in the sequence. As mentioned in Loop Cells(x,1) tell compiler to increase 1 in row and in column 1 i.e. A1, A2, A3…..A10.

 Example: To create worksheets Automatically (Named 1May, 2May, 3May…..10May) using For Loop

Sub Table()

Dim i As Integer

For i = 1 To 10

sheets.Add(After:=Worksheets(1)).Name = i & “May”

Next i

End Sub

92 thoughts on “VBA- Loops

  1. Hello there, I do believe your website may be having internet browser compatibility problems. Whenever I look at your blog in Safari, it looks fine but when opening in I.E., it’s got some overlapping issues. I simply wanted to give you a quick heads up! Other than that, excellent blog!|

  2. Aw, this was a really nice post. Taking a few minutes and actual effort to make a great article… but what can I say… I procrastinate a lot and don’t seem to get anything done.|

  3. Thank you a bunch for sharing this with all people you actually realize what you’re talking approximately! Bookmarked. Please additionally consult with my web site =). We may have a hyperlink trade contract among us|

  4. Admiring the dedication you put into your site and in depth information you present. It’s good to come across a blog every once in a while that isn’t the same out of date rehashed information. Excellent read! I’ve saved your site and I’m adding your RSS feeds to my Google account.|

  5. I blog frequently and I seriously thank you for your content. This great article has truly peaked my interest. I will take a note of your website and keep checking for new information about once per week. I subscribed to your Feed as well.|

  6. I am really loving the theme/design of your weblog. Do you ever run into any internet browser compatibility issues? A couple of my blog readers have complained about my site not operating correctly in Explorer but looks great in Safari. Do you have any suggestions to help fix this issue?|

  7. Great blog right here! Also your web site loads up very fast! What host are you the usage of? Can I am getting your associate link on your host? I want my web site loaded up as fast as yours lol|

  8. Hi there! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a marvellous job!|

  9. This design is incredible! You obviously know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Wonderful job. I really loved what you had to say, and more than that, how you presented it. Too cool!|

  10. I think this is one of the most important information for me. And i’m happy reading your article. But wanna commentary on few basic issues, The website style is wonderful, the articles is in point of fact nice : D. Excellent task, cheers|

  11. Awesome blog you have here but I was wanting to know if you knew of any discussion boards that cover the same topics talked about in this article? I’d really like to be a part of group where I can get suggestions from other knowledgeable individuals that share the same interest. If you have any suggestions, please let me know. Thanks!|

  12. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why throw away your intelligence on just posting videos to your weblog when you could be giving us something informative to read?|

  13. I’ll right away seize your rss as I can not find your email subscription hyperlink or e-newsletter service. Do you have any? Kindly permit me realize so that I may subscribe. Thanks.|

  14. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Bless you!|

  15. Aw, this was an incredibly nice post. Finding the time and actual effort to create a superb article… but what can I say… I procrastinate a whole lot and never manage to get nearly anything done.|

  16. I do agree with all of the ideas you’ve offered for your post. They’re very convincing and can certainly work. Nonetheless, the posts are too quick for newbies. May just you please extend them a little from next time? Thank you for the post.|

  17. Thank you for any other informative site. Where else could I get that type of info written in such a perfect way? I have a mission that I am just now operating on, and I’ve been on the look out for such information.|

  18. What’s Taking place i am new to this, I stumbled upon this I’ve discovered It positively helpful and it has helped me out loads. I am hoping to give a contribution & help different customers like its helped me. Good job.|

  19. I believe this is one of the so much important information for me. And i’m happy studying your article. However want to statement on few common things, The site style is wonderful, the articles is actually nice : D. Just right job, cheers|

  20. Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!|

  21. What i do not realize is actually how you are now not really much more neatly-favored than you may be now. You are very intelligent. You know thus considerably in the case of this matter, produced me personally consider it from a lot of numerous angles. Its like women and men don’t seem to be interested unless it is one thing to do with Girl gaga! Your own stuffs outstanding. Always handle it up!|

  22. It’s a pity you don’t have a donate button! I’d certainly donate to this outstanding blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to new updates and will share this website with my Facebook group. Talk soon!|

  23. I really like your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to design my own blog and would like to know where u got this from. appreciate it|

  24. Howdy! Do you know if they make any plugins to help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Cheers!|

  25. You are so interesting! I do not believe I’ve read through a single thing like this before. So great to discover someone with some original thoughts on this topic. Really.. thanks for starting this up. This website is something that’s needed on the web, someone with a little originality!|

  26. My partner and I stumbled over here different web address and thought I might as well check things out. I like what I see so now i am following you. Look forward to finding out about your web page repeatedly.|

  27. I do agree with all the concepts you’ve offered in your post. They are very convincing and will certainly work. Still, the posts are too brief for novices. May you please prolong them a little from subsequent time? Thank you for the post.|

  28. Spot on with this write-up, I really believe this site needs a great deal more attention. I’ll probably be returning to read through more, thanks for the advice!|

  29. Excellent post. I used to be checking constantly this weblog and I am impressed! Very helpful information particularly the last section 🙂 I take care of such info a lot. I was seeking this certain information for a very long time. Thank you and best of luck. |

  30. I was curious if you ever considered changing the structure of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or 2 pictures. Maybe you could space it out better?|

  31. Hi, Neat post. There’s an issue along with your site in internet explorer, might test this? IE nonetheless is the marketplace leader and a good part of folks will pass over your fantastic writing because of this problem.|

  32. I used to be suggested this website via my cousin. I’m no longer sure whether or not this put up is written by means of him as no one else understand such specified about my problem. You are wonderful! Thanks!|

  33. First off I want to say awesome blog! I had a quick question in which I’d like to ask if you don’t mind. I was interested to find out how you center yourself and clear your head before writing. I have had a tough time clearing my mind in getting my ideas out there. I truly do enjoy writing but it just seems like the first 10 to 15 minutes tend to be lost simply just trying to figure out how to begin. Any ideas or tips? Kudos!|

  34. This is really interesting, You’re an excessively professional blogger. I’ve joined your feed and stay up for looking for extra of your great post. Additionally, I have shared your site in my social networks|

  35. Woah! I’m really loving the template/theme of this blog. It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between usability and appearance. I must say you have done a great job with this. In addition, the blog loads super quick for me on Safari. Exceptional Blog!|

  36. Unquestionably imagine that which you said. Your favourite justification appeared to be at the internet the easiest factor to take note of. I say to you, I definitely get irked whilst folks consider concerns that they just do not realize about. You managed to hit the nail upon the top and also defined out the entire thing with no need side-effects , folks can take a signal. Will likely be back to get more. Thank you|

  37. Hey there are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and set up my own. Do you need any html coding expertise to make your own blog? Any help would be greatly appreciated!|

  38. Usually I do not read article on blogs, but I wish to say that this write-up very forced me to take a look at and do it! Your writing style has been surprised me. Thank you, very great post.|

  39. Right here is the perfect site for anybody who hopes to find out about this topic. You understand a whole lot its almost tough to argue with you (not that I personally would want to…HaHa). You definitely put a brand new spin on a subject that has been written about for decades. Excellent stuff, just wonderful!|

  40. I am extremely impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it’s rare to see a nice blog like this one nowadays.|

  41. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this hike.|

  42. Nice post. I used to be checking continuously this weblog and I’m inspired! Very useful info specially the last phase 🙂 I deal with such info a lot. I used to be looking for this particular info for a long time. Thanks and best of luck. |

  43. Cool blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your design. Thanks|

  44. Hi, i think that i saw you visited my website thus i came to “return the favor”.I’m trying to find things to enhance my web site!I suppose its ok to use a few of your ideas!!|

  45. My spouse and I stumbled over here from a different web address and thought I might check things out. I like what I see so now i am following you. Look forward to finding out about your web page yet again.|

  46. I absolutely love your website.. Great colors & theme. Did you develop this website yourself? Please reply back as I’m hoping to create my own personal blog and would like to find out where you got this from or exactly what the theme is called. Thank you!|

  47. Hi, Neat post. There is an issue together with your web site in web explorer, would check this? IE still is the marketplace chief and a good part of people will miss your magnificent writing because of this problem.|

Leave a Reply

Your email address will not be published.

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/04/05/vba-loops
YouTube49
YouTube