Dear members,
My queries simply do not wish to be executed when combined with a for loop or if a file is closed an saved after the query function (see the code below):
' Close files
'Filename_FROM.Close False
'Filename_TO.Close True
'Choose_Structure_5.Hide
TO_Sheet_CTO.Activate
'For i = 2 To Application.CountA(Range("B:B"))
Call CustomExpense
'TO_Sheet_CTO.Range("S2") = TO_Sheet_CTO.Range("AF2")
'TO_Sheet_CTO.Range("AF:AF").Delete
'Next i
End If
End Sub
Private Sub CustomExpense()
Dim sqlstring As String
Dim connstring As String
Dim qt As QueryTable
sqlstring = "select IXLCFP from IIX where IXITMN = '" & Cells(2, 2) & "'"
connstring = _
"ODBC;DSN=XY;UID=Y;PWD=atlas;Database=X"
With ActiveSheet.QueryTables.Add(Connection:=connstring, _
Destination:=Range("AF1"), Sql:=sqlstring)
.Refresh
End With
End Sub
I have tried puttig the query code in different areas of the original code and using the Wait method. Nothing works. I need to take item numbers, send them as a query and receive a corresponding price, which is saved into an Excel sheet. This needs to be done for 2 up to 100 (!) item numbers. Each item number is a seperate query; I have not combined them into a single query as you never know how many item numbers you have. It can be 2 one day, 50 tomorrow.
In MATLAB I have successfully used 1000 queries in a for loop with zero issues. I don't understand why the queries (1) take so much time with VBA and (2) why they get interuppted by surrounding events. Any thoughts?
Bookmarks