Cancel an external query in Excel VBA

I created an Excel spreadsheet that helps with data analysis from an Oracle database.

The user then enters the Refresh Query button, which generates a query to run Oracle. The request takes a minute or so to complete. Although VBA code does not hang on ".Refresh", all Excel windows remain frozen until the request completes.

Sub refreshQuery_click()
    Dim queryStr as String

    ' Validate parameters and generate query   
    ' ** Code not included **
    '

    ' Refresh Query
    With ActiveWorkbook.Connections("Connection").OLEDBConnection
        .CommandText = queryStr
        .Refresh
    End With
End Sub

Is there a way for the user to manually cancel the request (calling .CancelRefresh) when the Excel user interface is frozen?

, . Excel ( VBA) " " . Esc, Ctrl + Break script. , DoEvents ( .Refresh) .

+3
4

, . .

:

  • . .
  • Excel "".
  • () . , Excel. , Excel , Excel , Excel . Excel.

:

  • , . , , , , . , , , .
  • , . , Windows ( ).

. , .

: C:\DataUpdate.xls

"DataUpdate.xls" C: \. A1 1 QueryTable, .

Option Explicit

Sub UpdateTable()

Dim ws As Worksheet
Dim qt As QueryTable

Set ws = Worksheets("Sheet1")
Set qt = ws.Range("A1").QueryTable

qt.Refresh BackgroundQuery:=False

End Sub

Sub OnWorkbookOpen()
Dim wb As Workbook
Set wb = ActiveWorkbook

'I put this If statement in so I can change the file's
'name and then edit the file without code
'running.  You may find a better way to do this.
If ActiveWorkbook.Name = "DataUpdate.xls" Then


    UpdateTable
    'I update a cell in a different sheet once the update is completed.
    'I'll check this cell from the "user workbook" to see when the data been updated.
    Sheets("Sheet2").Range("A1").Value = "Update Table Completed    " & Now()
    wb.Save

    Application.Quit

End If


End Sub

ThisWorkbook Excel , Workbook_Open(). : .

Private Sub Workbook_Open()
OnWorkbookOpen

End Sub

. , , 1) , 2) ​​ Office Live. ​​ Office Live, .

: C:\RunExcel.bat

, Excel, . , Excel , Excel , , , ( , Excel.exe "c:\File.xls"). , , , , , . Excel.

:

cd "C:\Program Files\Microsoft Office\Office10\" 
Excel.exe "C:\DataUpdate.xls"

Windows Scripting, , Excel. .

: C:\UserWorkbook.xls

, " ". , , .

, "Update Table Completed" DataUpdate. G1 Sheet1 .

VBA :

Option Explicit

Sub UpdateOtherWorkbook()
Dim strFilePath As String
Dim intOpenMode As Integer
Dim strCallPath As String
Dim strCellValue As String
Dim strCellFormula As String
Dim ws As Worksheet
Dim rng As Range

Set ws = Worksheets("Sheet1")
Set rng = ws.Range("G1")
strCellFormula = "='C:\[DataUpdate.xls]Sheet2'!A1"
'This makes sure the formula has the most recent "Updated" value
'from the data file.
rng.Formula = strCellFormula

strFilePath = "C:\RunExcel.bat"
intOpenMode = vbHide

'This will call the batch file that calls the Excel file.
'Since the batch file executes it code and then closes,
'the Excel file will be able to keep running.
Shell strFilePath, intOpenMode

'This method, defined below, will alert the user with a
'message box once the update is complete. We know that
'the update is complete because the "Updated" value will
'have changed in the Data workbook.
AlertWhenChanged

End Sub

Sub AlertWhenChanged()

Dim strCellValue As String
Dim strUpdatedCellValue As String
Dim strCellFormula As String
Dim ws As Worksheet
Dim rng As Range

Set ws = Worksheets("Sheet1")
Set rng = ws.Range("G1")
strCellFormula = "='C:\[DataUpdate.xls]Sheet2'!A1"

strCellValue = rng.Value
strUpdatedCellValue = strCellValue

'This will check every 4 seconds to see if the Update value of the
'Data workbook has been changed.  MyWait is included to make sure
'we don't try to access the Data file while it is inaccessible.
'During this entire process, the user is still able to work.
Do While strCellValue = strUpdatedCellValue
    MyWait 2
        rng.Formula = strCellFormula
    MyWait 2
        strUpdatedCellValue = rng.Value
    DoEvents
Loop

MsgBox "Data Has Been Updated!"

End Sub

Sub MyWait(lngSeconds As Long)
Dim dtmNewTime As Date

dtmNewTime = DateAdd("s", lngSeconds, Now)

Do While Now < dtmNewTime
    DoEvents
Loop


End Sub

, " ", , ​​ . , , . , , .

, Excel . .

!

+4

EDIT: , , , . ( , )

VBA, Ctrl + Break . , , , . , .

Excel ( , "" > "" Excel), , , , , . Excel .

, DoEvents, , . , , . , , ( , ).


, , , , . Excel . DoEvents , . Excel - script, . - , , . , .

+1

, - Do Events .

0

XLLoop. excel (UDfs) . (, Java, Ruby, Python, PHP).

(, , ) .

XLL "" , ( ).

, , , - .

0

Source: https://habr.com/ru/post/1722515/


All Articles