Question : Run macro in background without blocking Excel input / output

Dear experts,

I have the following VBA code, which tests the connection to an Essbase server, after asking the user for input on server name, database name, username and password. I want to be able to run it in the background after user input, so it tries every minute to connect to that database and lets me know when the connection is successful.

The connection in Excel is done through an add-in.

Of course, I want to do this without blocking excel activity, as the code could be running for a few minutes or more.

Can I do this and how?

Thanks,

Thomas
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
Declare Function EssVConnect Lib "ESSEXCLN.XLL" (ByVal SheetName As Variant, ByVal username As Variant, ByVal Password As Variant, ByVal server As Variant, ByVal application As Variant, ByVal database As Variant) As Long
Declare Function EssVRetrieve Lib "ESSEXCLN.XLL" (ByVal SheetName As Variant, ByVal range As Variant, ByVal lockFlag As Variant) As Long
Declare Function EssVDisconnect Lib "ESSEXCLN.XLL" (ByVal SheetName As Variant) As Long
Declare Function EssVSendData Lib "ESSEXCLN.XLL" (ByVal SheetName As Variant, ByVal range As Variant) As Long
Declare Function EssVUnlock Lib "ESSEXCLN.XLL" (ByVal SheetName As Variant) As Long
Declare Function EssVZoomIn Lib "ESSEXCLN.XLL" (ByVal SheetName As Variant, ByVal range As Variant, ByVal selection As Variant, ByVal level As Variant, ByVal across As Variant) As Long
Declare Function EssVSetSheetOption Lib "ESSEXCLN.XLL" (ByVal SheetName As Variant, ByVal item As Variant, ByVal sheetOption As Variant) As Long
Dim strSingleCheck As String
 
Sub TestTopazConnection()
Dim sht As Worksheet, arrShtNames(0 To 256) As String
Dim i As Integer, j As Integer
Dim arrConnDetails As Variant
 
application.Calculation = xlCalculationManual
 
arrConnDetails = GetConnectDetails()
 
i = 0
 
Do While ConnectOnly(arrShtNames(j), _
            CStr(arrConnDetails(1)), CStr(arrConnDetails(2)), CStr(arrConnDetails(3)), _
            CStr(arrConnDetails(4)), CStr(arrConnDetails(5)), True) = True
 
application.Wait DateAdd("s", 10, Now)
Loop
    
MsgBox ("Connect successful")
 
application.Calculation = xlCalculationAutomatic
 
End Sub
 
Function ConnectOnly(strSheet As String, strPassword As String, strUser As String, strServer As String, strApp As String, strDB As String, blDisconnect As Boolean, Optional strRange As String) As Boolean
 
Dim x As Long, strMessage As String
 
x = EssVConnect(strSheet, strUser, strPassword, strServer, strApp, strDB)
 
If x <> 0 Then
    ConnectOnly = False
    Exit Function
Else
    ConnectOnly = True
End If
 
If blDisconnect = True Then
    x = EssVDisconnect(strSheet)
End If
 
End Function

Answer : Run macro in background without blocking Excel input / output

I think that the best that can be done is to open another instance of Excel and work on it. Of course this means that you cannot work on the files open in the instance of excel running the macro.

Saqib
Random Solutions  
 
programming4us programming4us