Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-threading in VBA

Does anybody here know how to get VBA to run multiple threads? I am using Excel.

like image 434
KJ Saxena Avatar asked Apr 19 '11 19:04

KJ Saxena


People also ask

Can you multithread in VBA?

VBA is built in a single-threaded apartment. The only way to get multiple threads is to build a DLL in something other than VBA that has a COM interface and call it from VBA.

How do you do multithread in Excel?

To enable the multi-threading feature, click the FILE tab and select Options to open the Excel Options dialog box, as mentioned earlier. Click Advanced in the menu on the left. Scroll down to the Formulas section and select the Enable multi-threaded calculation check box so there is a check mark in the box.

What is multi-threading explain with example?

What is MultiThreading? Multithreading enables us to run multiple threads concurrently. For example in a web browser, we can have one thread which handles the user interface, and in parallel we can have another thread which fetches the data to be displayed. So multithreading improves the responsiveness of a system.

What is multiple threading?

Multithreading is the ability of a program or an operating system to enable more than one user at a time without requiring multiple copies of the program running on the computer. Multithreading can also handle multiple requests from the same user.


2 Answers

Can't be done natively with VBA. VBA is built in a single-threaded apartment. The only way to get multiple threads is to build a DLL in something other than VBA that has a COM interface and call it from VBA.

INFO: Descriptions and Workings of OLE Threading Models

like image 73
Thomas Avatar answered Oct 08 '22 21:10

Thomas


As you probably learned VBA does not natively support multithreading but. There are 3 methods to achieve multithreading:

  1. COM/dlls - e.g. C# and the Parallel class to run in separate threads
  2. Using VBscript worker threads - run your VBA code in separate VBscript threads
  3. Using VBA worker threads executed e.g. via VBscript - copy the Excel workbook and run your macro in parallel.

I compared all thread approaches here: http://analystcave.com/excel-multithreading-vba-vs-vbscript-vs-c-net/

Considering approach #3 I also made a VBA Multithreading Tool that allows you to easily add multithreading to VBA: http://analystcave.com/excel-vba-multithreading-tool/

See the examples below:

Multithreading a For Loop

Sub RunForVBA(workbookName As String, seqFrom As Long, seqTo As Long)     For i = seqFrom To seqTo         x = seqFrom / seqTo     Next i End Sub  Sub RunForVBAMultiThread()     Dim parallelClass As Parallel       Set parallelClass = New Parallel       parallelClass.SetThreads 4       Call parallelClass.ParallelFor("RunForVBA", 1, 1000)  End Sub 

Run an Excel macro asynchronously

Sub RunAsyncVBA(workbookName As String, seqFrom As Long, seqTo As Long)     For i = seqFrom To seqTo         x = seqFrom / seqTo     Next i End Sub  Sub RunForVBAAndWait()     Dim parallelClass As Parallel      Set parallelClass  = New Parallel      Call parallelClass.ParallelAsyncInvoke("RunAsyncVBA", ActiveWorkbook.Name, 1, 1000)      'Do other operations here     '....      parallelClass.AsyncThreadJoin  End Sub 
like image 32
AnalystCave.com Avatar answered Oct 08 '22 21:10

AnalystCave.com