I am working on a small excel project where I have a user form. The user form has a combo box that has a list of company names retrieved from column (A) in an excel worksheet (This works as expected).
The form has a text box, that depending on the selection from the drop down box returns the stock ticker from column B (Works as expected).
The next step is where it breaks down. The stock ticker value is then passed to a web query that connects to yahoo finance, and retrieves data from the site.
Problem 1: The web query does not return data until the form is closed. I want it to return the values "instantly."
Problem 2: Each time I run the query, a new query table is built, even though I have coded my script to delete query tables.
Private Sub cb_Stock_Name_Change()
Set ws = Worksheets("Stock_Info")
With Me
.tb_ticker.Value = ws.cells(.cb_stock_name.ListIndex + 2, 2)
'.TextBox3.Value = Format(Sheet1.cells(.ComboBox1.ListIndex + 7, 9), "0%")
'.TextBox2.Value = Format(Sheet1.cells(.ComboBox1.ListIndex + 7, 10), "0%")
End With
Dim ticker As String
Dim conn As String
Set ws_query = Worksheets("Stock_Query")
ticker = tb_ticker.Value
conn = "URL;http://finance.yahoo.com/q?s=" & ticker
Dim qt As QueryTable
For Each qt In ws_query.QueryTables
qt.Delete
Next qt
Set qt = ws_query.QueryTables.Add _
(Connection:=conn, Destination:=Range("A1"))
With qt
'.Connection = conn
'.Destination = Range("A1")
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebSelectionType = xlSpecifiedTables
.WebTables = "2"
.Refresh
End With
With Me
.tb_previous_close.Value = ws_query.cells(1, 2)
End With
End Sub
Questions: What is wrong with my code that is a) doesn't return until my form is closed b) doesn't delete the previous querytable?
Problem 1 - resolved as per the comments (need to put form property as Modeless). You can check MSDN for details on form's mode properties. FYI, by default the forms are modal.
Problem 2 - You need to specify qt's .Name property. Sample this
Let me know if this works for you