Can I run two macros in the same time?
Thank for the help
Emil.
Can I run two macros in the same time?
Thank for the help
Emil.
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.
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.
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.
>
>
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.
>
>
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.
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.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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.
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.
I think you missed the point that they are not in fact concurrent, since VBA is single threaded.
Rory
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!
If you say so.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks