ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Can I run two macros in the same time? (https://www.excelbanter.com/excel-programming/364724-can-i-run-two-macros-same-time.html)

emil

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


AA2e72E

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.

witek

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.


emil

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.



Jim Thomlinson

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.



witek

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.



















Jim Thomlinson

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.




















witek

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.


All times are GMT +1. The time now is 01:11 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com