Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 33

Thread: Need help with Question, is it possible to Call modules to work or loop through?

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location

    Need help with Question, is it possible to Call modules to work or loop through?

    I have modules 7,8,9,10,11,12 that are Subs

    Using the Code:
     
    Sub MasterMacro()
         Call Macro7
          Call Macro8
          Call Macro9
          Call Macro10
          Call Macro11
          Call Macro12
    End Sub
    I Believe each macro would run, one at a time, one by one; The question is, is there a way to run by calling these macros to repeat to a specific amount of times before it stops.

    Any help on this is Greatrly Appreciated!!!
    Thank you in Advance,
    Sincerely

    Dennis

  2. #2
    VBAX Contributor D_Marcel's Avatar
    Joined
    Feb 2012
    Location
    Tokyo
    Posts
    117
    Location
    Hello!
    I may be underestimating your request, but according to my understanding:

    [VBA]Sub Loop_Through_Subs()

    Dim varIndex As Integer
    Dim varTotal As Integer

    varTotal = 5 'Number of times your macros will be called inside the loop
    For varIndex = 1 To varTotal
    'Call Macro1
    'Call Macro2
    'Call Macro3
    'Call Macro4
    Next varIndex


    End Sub[/VBA]

    Regards,

    Douglas
    "The only good is knowledge and the only evil is ignorance". Socrates

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    if I want the Macros 7,8,9,10,11,12 one at a time meaning;

    7 runs then, 8 runs, then 9 runs, then 10 runs, then 11 runs, then 12 runs then It would go and do this over again about over 8000 times then stop.

  4. #4
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    for i = 7 to 8000
     for j = 1 to i
       call macro7
       call macro8, etc
    .... etc
     next j
    next i

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    Awsome!!! Ill Give it a test Thank You!!!!!!
    I appreciate it!!!

    so it should look like this? so this is how it should look in regards to settin this up to work?

    SubMasterMacro()
    For i=7 to 8000
         For j = 1 To i
              Call macro7
              Call macro8
              Call macro9
              Call macro10
              Call macro11
              Call macro12
         Next j
    Next i
    Last edited by estatefinds; 08-13-2017 at 04:09 PM.

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    More simply
    For 1 = 1 to 8000
    Call MasterMacro
    Next i
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    I hope you realise that it will take a long time to run, since you will be running the macros 7+8+ 9 + all numbers up to 8000 times. I put it in because that is what you asked for.

    So you might want to try setting the i loop limit to something sensible like 20 to start with.

  8. #8
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    I understand it will take a long time. all macros 7,8,9,10,11,12 take a total of about 2 minutes.
    Just want to be sure Im setting it up correctly?
    SubMasterMacro()
    For i=7 To 8000
    For j = 1 To i
    Call macro7
    Call macro8
    Call macro9
    Call macro10
    Call macro11
    Call macro12
    Next j
    Next i

  9. #9
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    Your requirements are not totally clear: After you have run all the macros 7 times, are you thinking that you might do something ( like record a result) before you go on to run all the macros 8 times? And then similarly do you want to record are result after 8 times through the macros. If you aren't doing this , then MdMackillop's or d Marcel's solution will run all the macros 8000 times. If you want to record a result in between each set of runs through macros then you need a solution like mine. If you want to record results between each set of runs you would do it like this:

    SubMasterMacro() 
    For i=7 To 8000 
        For j = 1 To i 
            Call macro7 
            Call macro8 
            Call macro9 
            Call macro10 
            Call macro11 
            Call macro12 
        Next j 
    ' write code to record results between runs here 
    Next i

  10. #10
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    ok sorry for the unclarity,

    So I have the modules 7,8,9,10,11,12.

    I want to run these one at a time;
    run 7 then it ends, then 8 will start then end, then 9 will start and then end, then 10 will start then end, then 11 will start then end, then finally 12 will start then end.
    Then I need it to repeat this process for about 8000 times or what ever amount I need it to.

    so the end result:
    each macro records a result that finally leads to a final result. with the fianl macro.

    so each macro performs a task and results it. so it automatically as i do it now shows the result.

    So instead of doing it manually running each macro as i do now,
    I would open ALT F8 then run then when thats done it would go to next one until it finishes and so on.

    Then I would do it all over again.

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here is a simple example. Run Test on a blank worksheet
    Dim j
    
    Sub Test()
    For j = 1 To 3
    Call MasterMacro
    Next
    End Sub
    
    Sub MasterMacro()
        Call Macro7
        Call Macro8
        Call Macro9
        Call Macro10
        Call Macro11
        Call Macro12
    End Sub
    
    Sub Macro7()
    For i = 1 To 100000
    Cells(i, 1) = i * j
    Next i
    End Sub
    
    Sub Macro9()
    For i = 1 To 100000
    Cells(i, 3) = i * j
    Next i
    End Sub
    
    Sub Macro11()
    For i = 1 To 100000
    Cells(i, 5) = i * j
    Next i
    End Sub
    
    Sub Macro8()
    For i = 1 To 3
    Cells(i, 2) = i * j
    Next i
    End Sub
    
    Sub Macro10()
    For i = 1 To 3
    Cells(i, 4) = i * j
    Next i
    End Sub
    
    Sub Macro12()
    For i = 1 To 3
    Cells(i, 6) = i * j
    Next i
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    the codes that I run depends on the others result.

    So I cant Have the first Code 7 run for example 8000 times then have code 8 run 8000 times etc.



    They have to be run consecutively 7 then it ends, then 8 then it ends, then 9 then it ends , then 10 then it ends then 11 then it ends then 12 then it ends then repeat.


    until it has done this for about 8000 times.

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    And your question is?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Read MD's entire post. That code runs each of his macro#'s one after the other... 8000 times.
    See how long it takes.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  15. #15
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    so the code above in post 11 should do this?

    Im testing now,
    just ran it and it replaced my data in column C with "0"


    Ok i ran it again and still it turns my data to zero's

    I tried also only 10 rows and it still puts zeros where my data is.

    it has never done this I copied and pasted the code in post #11 exactly
    Last edited by estatefinds; 08-13-2017 at 05:47 PM.

  16. #16
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    im having trouble,
    I even tried doing the code again an is not working correctly I even tried replacing the "Macro" in the Code to "Module" as the 7 8 9 10 11 12 are Modules.
    When i entered my codes I inserted as modules.

    the Dim j
    Should be under the Sub. I thought that looked odd being its declaring variable should be between Subs.


    I ran again and is keeps placing zeros in my data.

    its not working like when i do it manually

    I aslo rearranged the 7 8 9 10 11 12 to go in order in your code and still it isnt working. Im not sure why its not working.
    Im stumped.

    if i cant do it this way with call module is there a way to place each code in order under the other?


    This might help in regards to macro name and Module number

    Sub Test () is module7 (code)
    Sub testA_v2() is Module8 (code)
    Sub test2 () is Module9 (code)
    Sub SplitAddress() is Module10(code)
    Sub Test_v3() is Module11 (code)
    Sub sbClearCells() is Module12 (code)
    Last edited by estatefinds; 08-13-2017 at 07:23 PM.

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is not modules that you run, they are just containers for procedures, but the procedures themselves. MD showed you exactly what you wanted in post #11, just replace Macro7 with Test, Macro 8 with testA_v2, etc.. The problem with 0s would seem to be your code, there is nothing in MDs code that sets anything, and you never mentioned setting to 0 before.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  18. #18
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    I understand it will take a long time. all macros 7,8,9,10,11,12 take a total of about 2 minutes.
    a quick calculation one iteration takes two minutes, so 8000 interations is going to take:

    2 * 8000= 16000 minutes = 266.66 hours = 11.11 days

    Before I tried running a test like that , I would seriously look at trying to speed up macros7 to 12. I would be very surprised if we couldn't help you with that , but we would need to see the macros

  19. #19
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    the Dim j
    Should be under the Sub. I thought that looked odd being its declaring variable should be between Subs.
    Your problem with the zeros is because that the "Dim j" MUST be right at the top of the module before any of the "subs". This defines it as a varialbe which is the same variable where ever it is used within that module. if you look at Md code you will see that each sub used "j" . if it is defined at the top of the module all of the "j"s are the same variable and thus the value gets carried across from sub to sub. If it is not defined at the top then each subroutine creates it's own J , and all but one of them (test) are blank, hence the zeros.
    Last edited by offthelip; 08-14-2017 at 03:29 AM. Reason: Explanation

  20. #20
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    The zeros were appearing when I copied code exactly with dim directly above subs that is when I got the zeros, so I thought it was because of location of Dim

Posting Permissions

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