+ Reply to Thread
Results 1 to 11 of 11

For loop and change a variable

  1. #1
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    For loop and change a variable

    Hi All,

    I have this for loop
    Please Login or Register  to view this content.
    I am wondering if there was a way to change the variable Dog each time in the loop. I think i should be able to do this with an array but i cannot seem to get it. I believe my new code would look something like this.

    Please Login or Register  to view this content.
    I am not sure if it is something as simple as taking the "" out of the array or what. Is there some way to change a variable each time the for loop is run? Any help is appretiated. Thanks
    Last edited by amotto11; 02-08-2013 at 12:53 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: For loop and change a variable

    What do you want to change it to?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: For loop and change a variable

    I have edited my original post, to say j instead of Dog. In cell B2 there is a data validation drop down that relates to X2, if x2 is 2 then the data validation is dog, 3 is cat and 4 is mouse. so my real code looks like this, just a little different than before, please let me know if i can explain further
    Please Login or Register  to view this content.
    Every page from sheet 2 to sheetnumber is the same in terms of content, but the user can have different values per page in the data validation menu. That is why i need to loop through the pages and check to see if the number in x2 is 2, 3, or 4. I would like to loop through and check for 2 and put that in dog, then check for 3 and put in cat, then 4 and put in mouse.
    Last edited by amotto11; 02-08-2013 at 12:52 PM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: For loop and change a variable

    I'm a little confused.

    You say it's Dog you want to change, but you don't actually do anything with Dog in the code.

    Also, if you want to change it how so you want to change it?

    What is the code meant to do anyway?

    It seems to be counting something, but what?

  5. #5
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: For loop and change a variable

    I am sorry for the confusion, it is sometimes hard to change things to give a good example. I will try to write out what i am trying to do rather than give code.

    Step one is to look through all the pages in the workbook and determine what if any data validation is chosen. The data validation is in cell F2 and it records a number into cell X2, that relates to the value the user has chosen.

    I need to go through every page in a varying page workbook (SheetNumber) and look at cell X2 to see what the user has chosen. Then i need the program to average cell F10 in all the worksheets that are similar. Basically an averageif.

    The struggles are that the user can choose one of 9 things in the drop down menu and i need to average all like things. That is why i was using a loop to go through all the pages and look at Cell X2, if it was 2 then i wanted to count the number in F10 and store it in Dog, then counter would keep a runnning total of all of the pages that X2 value was 2. as the loop continues i would be adding up all of the F10's that have a 2 in X2 and divide that number by counter. this would give me in essence an average of the F10's with a 2 in X2. Then i need all of the F10's that have a 3 in X2 and divide them by the total pages that have a three in X3.

    I hope this makes sense. Really i am just calculating a running averageif. Thanks for your interest and any help you can give me.
    Last edited by amotto11; 02-08-2013 at 12:52 PM.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: For loop and change a variable

    I think I kind of get the idea.

    Could you attach a sample workbook?

  7. #7
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: For loop and change a variable

    Norie I have attached a very simple example. As you can see F10 contains the days between initial and end, i am trying to averageif the dates with the same animal in call F2, F2 relates to K2. Imagine there being 9 different animals, or whatever, to choose from and you need to averageif for all animals. The workbook length will change so a simple averageif statement will not work in the worksheet so i have to do this in vba, then place the number in Cell A1 on sheet 5. I have gone through and done the average by hand but i need it to be variable to change with the workbook. Thank youf or all of your help. I hope i have explained myself well enough here.
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: For loop and change a variable

    Norie,

    Is there anything that you can do for me? Thanks for the help.
    Regards,
    amotto

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)

  9. #9
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: For loop and change a variable

    I did some more research online and i believe that you cannot change variables in a loop, therefore since Norie could not answer my question i just used 18 different variables in my loop and am closing the thread, i know that this is not efficient but is all i could think to do.
    Last edited by amotto11; 02-11-2013 at 02:49 PM.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: For loop and change a variable

    amotto

    Sorry for not gettting back to you.

    Are you sure you actually need all those variables?

    Couldn't you store the running average/total for each category, eg Dog, Cat, in cells on a summary sheet?

    As you loop through the worksheets you update the relevant cell on the summary sheet.

    Or alternatively have 2 arrays, one for the categories and one for the averages.

    Then as you loop through the worksheets you lookup the selected category in the category array and update the correspinding item in the average array.

  11. #11
    Forum Contributor
    Join Date
    08-22-2011
    Location
    Texas, United States
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    516

    Re: For loop and change a variable

    Norie,

    That is a good idea, I will attempt to do that, thank you for your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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