+ Reply to Thread
Results 1 to 8 of 8

Using loops in VBA

  1. #1
    Registered User
    Join Date
    05-01-2012
    Location
    Australia
    MS-Off Ver
    excel 2010
    Posts
    7

    Using loops in VBA

    I had a look and got some answers. I have found out how to get into the VB code and create a command button. I want to get a series of numbers across a row (say, 1 to ten), with an empty cell between each number. I wrote this code line to get my first number:
    Please Login or Register  to view this content.
    and that worked, so i copied 9 more times and just changed the cell number and the number i wanted in it, eg
    Please Login or Register  to view this content.
    and that worked of course, but is obviously tedious and ridiculously slow if i wanted 1 to 100

    So, sorry for the simple question: but how do i get it to do a whole series of numbers with one command? I tried
    Please Login or Register  to view this content.
    and it didn't work. Tried dashes instead of the word to... then thought, oh heck, stupid as it looks in a forum, I'm going to ask... I'm thinking there is an If loop that would do it for me... am i on the right track?
    Last edited by Paul; 05-08-2012 at 05:54 PM. Reason: Added CODE tags. Please do so yourself in the future.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Using loops in VBA

    You could use something like the following. I added a few comments so you can see what numbers to change to alter the start/end numbers and starting column. You could also skip 3 cells instead of 2 by changing "j=j+2" to "j=j+3", for example.
    Please Login or Register  to view this content.
    Last edited by Paul; 05-08-2012 at 06:03 PM.

  3. #3
    Registered User
    Join Date
    05-01-2012
    Location
    Australia
    MS-Off Ver
    excel 2010
    Posts
    7

    Re: Using loops in VBA

    Hey, Paul! That worked a treat. I had to take out the sub and end sub when i pasted it into my command button... didn't like that there were two of those... I didnt realise you could dimension something just as Long. I see that j is the column so thats useful to know to just go j = j + 1 or 2 or 3 to get it to skip columns and i assume i can do the same thing with rows. I see its a For next loop that you created and i see that i can change the values so if i want to go up to say a hundred I can. I am trying to not just copy and paste but learn from you as I go. Im a bit hazy about the Application.ScreenUpdating command. does setting it to false at the beginning clear everything or something? and then setting it as true means it updates to the numbers I want?

    thanks again for helping me all the way from Wisconsin (I'm in Australia so we are going into winter but its probably not much colder than your summers
    I have more questions but I will try to work things out first and only bother you when i get stuck.
    cheers from downundah
    Last edited by Paul; 05-08-2012 at 06:03 PM.

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Using loops in VBA

    Hi Reef, glad it worked for you and that you understand how to update the variables as needed for your sheets. Sorry about not mentioning to remove my Sub/EndSub headers - it crossed my mind but I forgot by the time I hit reply.

    As for "Application.ScreenUpdating", setting it to False simply tells the application not to update the window until ScreenUpdating is set back to True. Everything is still running and calculating as normal, you just don't see it. It reduces screen flicker, and can actually dramatically increase the speed of loops (for longer data sets, not really something like this).
    Last edited by Paul; 05-08-2012 at 06:03 PM.

  5. #5
    Registered User
    Join Date
    05-01-2012
    Location
    Australia
    MS-Off Ver
    excel 2010
    Posts
    7

    Re: Using loops in VBA

    Yo Paul, ok the mystery of app.screenupdating is solved. Since i will be working with long sets down the track, that is useful to know.
    My next programming feat is a leetle more complicated.

    I have a series of numbers in column 3 that come from some studies of animals i have done over the years... I'm a biologist and I do a lot of watching and recording... basically the numbers 0 to ten and so on represent behaviours: 0 is doing nothing, 1 is moving around, 2 is eating, 3 drinking, 4 preening (if its birds I am watching), 5 is defecating 6 is copulating etc...

    What I want to know from a sequence is the subsequent action after each action. So in a sequence, what is the most likely action after eating? That is what your row of numbers is for... it goes through the column, takes the first number eg 0 and its followed by a 5, so it goes over to the column with 0 in row ten, goes down 2 rows and puts in the number 5... then it goes back round to the second number in the sequence, which must be 5 and looks at the third number eg 3... then it runs over to the column with 5 in it, goes down 2 and puts in the number 3 etc etc.

    So I studied the different kinda loops, decided I would use a for next loop, slept on the problem and had a go at the code this morning and this is what I came up with:
    Please Login or Register  to view this content.
    The line it doesnt like is: Sheet1.Cells(k, ii).Value = ll

    I tried it several different ways and really thought this should work but it didn't and I don't know why...but I bet somebody here does!

    Thanks again, Reef
    Last edited by reefswaggie; 05-10-2012 at 03:48 AM. Reason: Added CODE tags and spacing for readability.

  6. #6
    Registered User
    Join Date
    05-01-2012
    Location
    Australia
    MS-Off Ver
    excel 2010
    Posts
    7

    Re: Using loops in VBA

    Hi Paul,

    No one has enlightened me on what is wrong with my code:

    Please Login or Register  to view this content.
    could you help me out please? I hope its not something too stupid but I am a bit stumped. It looks ok to me
    thanks, reef

  7. #7
    Registered User
    Join Date
    05-11-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Using loops in VBA

    did you check any spaces between your "." ?

  8. #8
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Using loops in VBA

    reefswaggie,

    I'm not convinced you need VBA for this task. Attached is an example workbook based on the criteria you described.

    In column C is a list of random numbers between 0 and 9 going from C5 to C1004 (so there are 1000 random numbers acting as Action ID's as you described).

    In cell F11 and copied over and down to F211 is this array formula:
    Please Login or Register  to view this content.
    Note that array formulas must be entered with Ctrl+Shift+Enter and not just Enter.

    That array formula gets the following action ID's for each action. So it gets every action ID that followed action 0 from the list in column C.

    Then, to show the most common action that came after each action ID, cell F8 and copied over contains this regular (non-array) formula:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1