+ Reply to Thread
Results 1 to 12 of 12

Can I run two macros in the same time?

  1. #1
    emil
    Guest

    Can I run two macros in the same time?

    Can I run two macros in the same time?
    Thank for the help
    Emil.


  2. #2
    AA2e72E
    Guest

    RE: Can I run two macros in the same time?


    If you mean can one Macro call another before it is finished, yes, try:

    Sub ABC()
    ' code
    XYZ
    'code
    End Sub

    Sub XYZ()
    'code
    End Sub

    If you mean can two macros run concurrently, the answer is no.

  3. #3
    witek
    Guest

    Re: Can I run two macros in the same time?

    AA2e72E wrote:
    > If you mean can one Macro call another before it is finished, yes, try:
    >
    > Sub ABC()
    > ' code
    > XYZ
    > 'code
    > End Sub
    >
    > Sub XYZ()
    > 'code
    > End Sub
    >
    > If you mean can two macros run concurrently, the answer is no.



    The answer is yes.
    ------------------------------------------
    Module1:

    Sub test()
    UserForm1.Show
    UserForm2.Show
    Dim i As Integer
    For i = 1 To 10
    Debug.Print "Test: " & i
    DoEvents
    Next i
    Unload UserForm1
    Unload UserForm2
    End Sub
    ------------------------------------------
    UserForm1:

    Private Sub UserForm_Activate()
    Dim i As Integer
    For i = 1 To 10
    Debug.Print "UserForm1: " & i
    DoEvents
    Next i
    End Sub

    ------------------------------------------
    UserForm2:
    Private Sub UserForm_Activate()
    Dim i As Integer
    For i = 1 To 10
    Debug.Print "UserForm2: " & i
    DoEvents
    Next i
    End Sub
    ------------------------------------------

    In both userforms ShowModal property is set to false.

    Below is output. Do you see concurrency ?

    Test: 1
    UserForm1: 1
    UserForm2: 1
    UserForm2: 2
    UserForm2: 3
    UserForm2: 4
    UserForm2: 5
    UserForm2: 6
    UserForm2: 7
    UserForm2: 8
    UserForm2: 9
    UserForm2: 10
    UserForm1: 2
    UserForm1: 3
    UserForm1: 4
    UserForm1: 5
    UserForm1: 6
    UserForm1: 7
    UserForm1: 8
    UserForm1: 9
    UserForm1: 10
    Test: 2
    Test: 3
    Test: 4
    Test: 5
    Test: 6
    Test: 7
    Test: 8
    Test: 9
    Test: 10



    Windows in its nature is multithreading, so if you are able to split
    macors into different threads it can be multithreading.
    I don't know how much it is usefull in practice.


  4. #4
    emil
    Guest

    Re: Can I run two macros in the same time?

    Thank. Now, I have many options from you which I can try. Right, I can
    consider you my unseen friends. Best regards for all.
    Emil


    "witek" a scris:

    > AA2e72E wrote:
    > > If you mean can one Macro call another before it is finished, yes, try:
    > >
    > > Sub ABC()
    > > ' code
    > > XYZ
    > > 'code
    > > End Sub
    > >
    > > Sub XYZ()
    > > 'code
    > > End Sub
    > >
    > > If you mean can two macros run concurrently, the answer is no.

    >
    >
    > The answer is yes.
    > ------------------------------------------
    > Module1:
    >
    > Sub test()
    > UserForm1.Show
    > UserForm2.Show
    > Dim i As Integer
    > For i = 1 To 10
    > Debug.Print "Test: " & i
    > DoEvents
    > Next i
    > Unload UserForm1
    > Unload UserForm2
    > End Sub
    > ------------------------------------------
    > UserForm1:
    >
    > Private Sub UserForm_Activate()
    > Dim i As Integer
    > For i = 1 To 10
    > Debug.Print "UserForm1: " & i
    > DoEvents
    > Next i
    > End Sub
    >
    > ------------------------------------------
    > UserForm2:
    > Private Sub UserForm_Activate()
    > Dim i As Integer
    > For i = 1 To 10
    > Debug.Print "UserForm2: " & i
    > DoEvents
    > Next i
    > End Sub
    > ------------------------------------------
    >
    > In both userforms ShowModal property is set to false.
    >
    > Below is output. Do you see concurrency ?
    >
    > Test: 1
    > UserForm1: 1
    > UserForm2: 1
    > UserForm2: 2
    > UserForm2: 3
    > UserForm2: 4
    > UserForm2: 5
    > UserForm2: 6
    > UserForm2: 7
    > UserForm2: 8
    > UserForm2: 9
    > UserForm2: 10
    > UserForm1: 2
    > UserForm1: 3
    > UserForm1: 4
    > UserForm1: 5
    > UserForm1: 6
    > UserForm1: 7
    > UserForm1: 8
    > UserForm1: 9
    > UserForm1: 10
    > Test: 2
    > Test: 3
    > Test: 4
    > Test: 5
    > Test: 6
    > Test: 7
    > Test: 8
    > Test: 9
    > Test: 10
    >
    >
    >
    > Windows in its nature is multithreading, so if you are able to split
    > macors into different threads it can be multithreading.
    > I don't know how much it is usefull in practice.
    >
    >


  5. #5
    Jim Thomlinson
    Guest

    Re: Can I run two macros in the same time?

    No. I do not see concurrency. I see one process start another process. That
    process completes and then the original process continues. No
    multi-threading. No instances of both processes running at the same time
    writing to the immedaite window.
    --
    HTH...

    Jim Thomlinson


    "witek" wrote:

    > AA2e72E wrote:
    > > If you mean can one Macro call another before it is finished, yes, try:
    > >
    > > Sub ABC()
    > > ' code
    > > XYZ
    > > 'code
    > > End Sub
    > >
    > > Sub XYZ()
    > > 'code
    > > End Sub
    > >
    > > If you mean can two macros run concurrently, the answer is no.

    >
    >
    > The answer is yes.
    > ------------------------------------------
    > Module1:
    >
    > Sub test()
    > UserForm1.Show
    > UserForm2.Show
    > Dim i As Integer
    > For i = 1 To 10
    > Debug.Print "Test: " & i
    > DoEvents
    > Next i
    > Unload UserForm1
    > Unload UserForm2
    > End Sub
    > ------------------------------------------
    > UserForm1:
    >
    > Private Sub UserForm_Activate()
    > Dim i As Integer
    > For i = 1 To 10
    > Debug.Print "UserForm1: " & i
    > DoEvents
    > Next i
    > End Sub
    >
    > ------------------------------------------
    > UserForm2:
    > Private Sub UserForm_Activate()
    > Dim i As Integer
    > For i = 1 To 10
    > Debug.Print "UserForm2: " & i
    > DoEvents
    > Next i
    > End Sub
    > ------------------------------------------
    >
    > In both userforms ShowModal property is set to false.
    >
    > Below is output. Do you see concurrency ?
    >
    > Test: 1
    > UserForm1: 1
    > UserForm2: 1
    > UserForm2: 2
    > UserForm2: 3
    > UserForm2: 4
    > UserForm2: 5
    > UserForm2: 6
    > UserForm2: 7
    > UserForm2: 8
    > UserForm2: 9
    > UserForm2: 10
    > UserForm1: 2
    > UserForm1: 3
    > UserForm1: 4
    > UserForm1: 5
    > UserForm1: 6
    > UserForm1: 7
    > UserForm1: 8
    > UserForm1: 9
    > UserForm1: 10
    > Test: 2
    > Test: 3
    > Test: 4
    > Test: 5
    > Test: 6
    > Test: 7
    > Test: 8
    > Test: 9
    > Test: 10
    >
    >
    >
    > Windows in its nature is multithreading, so if you are able to split
    > macors into different threads it can be multithreading.
    > I don't know how much it is usefull in practice.
    >
    >


  6. #6
    witek
    Guest

    Re: Can I run two macros in the same time?

    Jim Thomlinson wrote:
    > No. I do not see concurrency. I see one process start another process. That
    > process completes and then the original process continues. No
    > multi-threading. No instances of both processes running at the same time
    > writing to the immedaite window.



    Right, but let's try something else

    userform2 is run as "background" process printing something on a screen
    from time to time.
    ------------------------------------
    Private Sub UserForm_Activate()
    Dim i As Long
    For i = 1 To 1000000
    If i Mod 10000 = 0 Then Debug.Print "UserForm2: " & i
    DoEvents
    Next i
    End Sub
    --------------------------------------

    Action in userform1 is triggered by user. I moved it to Click event.

    Private Sub UserForm_Click()
    Dim i As Integer
    For i = 1 To 10
    Debug.Print "UserForm1: " & i
    DoEvents
    Next i
    End Sub

    --------------------------------------

    And these are results.

    Test: 1
    UserForm2: 10000
    UserForm2: 20000
    UserForm2: 30000
    UserForm2: 40000
    UserForm2: 50000
    UserForm2: 60000
    UserForm2: 70000
    UserForm2: 80000
    UserForm2: 90000
    UserForm2: 100000
    UserForm2: 110000
    UserForm1: 1
    UserForm1: 2
    UserForm1: 3
    UserForm1: 4
    UserForm1: 5
    UserForm1: 6
    UserForm1: 7
    UserForm1: 8
    UserForm1: 9
    UserForm1: 10
    UserForm2: 120000
    UserForm2: 130000
    UserForm2: 140000
    UserForm1: 1
    UserForm1: 2
    UserForm1: 3
    UserForm1: 4
    UserForm1: 5
    UserForm1: 6
    UserForm1: 7
    UserForm1: 8
    UserForm1: 9
    UserForm1: 10
    UserForm2: 150000
    UserForm2: 160000
    ..
    ..
    ..

    UserForm2 is interrupted by UserForm1, which can be a solution for that
    problem if we can somehow trigger event from UserForm1.
    I never said that I guarantee not starving.



















  7. #7
    Jim Thomlinson
    Guest

    Re: Can I run two macros in the same time?

    But you never can get two threads of exectution to run concurrently. I agree
    that you can interup the flow of execution to allow another process to run
    but you can not get both processes going at the same time. There is just one
    thread. It might be running this process or it might be running that process
    but there is never more than one thread. In order to get true multi-threading
    you need to move up to C/C++ or a similar language. VB just won't cut it.
    --
    HTH...

    Jim Thomlinson


    "witek" wrote:

    > Jim Thomlinson wrote:
    > > No. I do not see concurrency. I see one process start another process. That
    > > process completes and then the original process continues. No
    > > multi-threading. No instances of both processes running at the same time
    > > writing to the immedaite window.

    >
    >
    > Right, but let's try something else
    >
    > userform2 is run as "background" process printing something on a screen
    > from time to time.
    > ------------------------------------
    > Private Sub UserForm_Activate()
    > Dim i As Long
    > For i = 1 To 1000000
    > If i Mod 10000 = 0 Then Debug.Print "UserForm2: " & i
    > DoEvents
    > Next i
    > End Sub
    > --------------------------------------
    >
    > Action in userform1 is triggered by user. I moved it to Click event.
    >
    > Private Sub UserForm_Click()
    > Dim i As Integer
    > For i = 1 To 10
    > Debug.Print "UserForm1: " & i
    > DoEvents
    > Next i
    > End Sub
    >
    > --------------------------------------
    >
    > And these are results.
    >
    > Test: 1
    > UserForm2: 10000
    > UserForm2: 20000
    > UserForm2: 30000
    > UserForm2: 40000
    > UserForm2: 50000
    > UserForm2: 60000
    > UserForm2: 70000
    > UserForm2: 80000
    > UserForm2: 90000
    > UserForm2: 100000
    > UserForm2: 110000
    > UserForm1: 1
    > UserForm1: 2
    > UserForm1: 3
    > UserForm1: 4
    > UserForm1: 5
    > UserForm1: 6
    > UserForm1: 7
    > UserForm1: 8
    > UserForm1: 9
    > UserForm1: 10
    > UserForm2: 120000
    > UserForm2: 130000
    > UserForm2: 140000
    > UserForm1: 1
    > UserForm1: 2
    > UserForm1: 3
    > UserForm1: 4
    > UserForm1: 5
    > UserForm1: 6
    > UserForm1: 7
    > UserForm1: 8
    > UserForm1: 9
    > UserForm1: 10
    > UserForm2: 150000
    > UserForm2: 160000
    > ..
    > ..
    > ..
    >
    > UserForm2 is interrupted by UserForm1, which can be a solution for that
    > problem if we can somehow trigger event from UserForm1.
    > I never said that I guarantee not starving.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >


  8. #8
    witek
    Guest

    Re: Can I run two macros in the same time?

    Jim Thomlinson wrote:
    > But you never can get two threads of exectution to run concurrently. I agree
    > that you can interup the flow of execution to allow another process to run
    > but you can not get both processes going at the same time. There is just one
    > thread. It might be running this process or it might be running that process
    > but there is never more than one thread. In order to get true multi-threading
    > you need to move up to C/C++ or a similar language. VB just won't cut it.


    Right.
    I checked processes.
    Excel doesn't create separate thread for user forms.

    You can create another Excel object to create another process, but it
    complicates communication between macros.

  9. #9
    Registered User
    Join Date
    10-31-2020
    Location
    US
    MS-Off Ver
    2019
    Posts
    5

    Re: Can I run two macros in the same time?

    Huge thank you for this concept;
    Running a sub that calls 2 forms to run background calculations--

    I don't care about how the processor handles it, but the fact that you can have 3 concurrent calculations running is phenomenal...

    In fact, it appears you can run the user forms in multiple instances....

    '------------------------------------------
    'Module1:
    Sub testuserformsconcurrently()
    UserForm1.Show (False)
    UserForm2.Show (False)
    UserForm1.Show (False)
    UserForm2.Show (False)
    UserForm1.Show (False)
    UserForm2.Show (False)
    UserForm1.Show (False)
    UserForm2.Show (False)
    UserForm1.Show (False)
    UserForm2.Show (False)
    UserForm1.Show (False)
    UserForm2.Show (False)
    UserForm1.Show (False)
    UserForm2.Show (False)
    UserForm1.Show (False)
    UserForm2.Show (False)
    Dim i As Integer
    For i = 1 To 10
    Debug.Print "Test: " & i
    DoEvents
    Next i
    Unload UserForm1
    Unload UserForm2
    End Sub
    '----------------------


    That was a lot of fun!
    I look forward to breaking excel even more.

  10. #10
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent SAC
    Posts
    8,885

    Re: Can I run two macros in the same time?

    I think you missed the point that they are not in fact concurrent, since VBA is single threaded.
    Rory

  11. #11
    Registered User
    Join Date
    10-31-2020
    Location
    US
    MS-Off Ver
    2019
    Posts
    5

    Re: Can I run two macros in the same time?

    What is exciting about it... is that if you keep the counts short enough, it gives the feel/impression of running multiple codes at once; taking advantage of the breaking at the start, and the simultaneous flow at the end one after the other, allowing multiple different types of calculations to run in quick succession;
    So while it seems true, you can't physically run two codes at once; you can give the impression of it using rapid succession and interrupts...

    Please Login or Register  to view this content.



    It's quite beautiful!

  12. #12
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent SAC
    Posts
    8,885

    Re: Can I run two macros in the same time?

    If you say so.

+ 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