How can I import SQL data through VBA without the headings?

ransomedbyfire

Board Regular
Joined
Mar 9, 2011
Messages
121
I am using VBA to pull data from an SQL database. I would like to run multiple queries back to back, looping through a column and plugging the value in each cell into an SQL query each time. Something like this:

Code:
dividendscount = Sheet5.Range("a:a").Find(what:="", after:=[a1]).Row - 2
If dividendscount = 0 Then
    GoTo nexti
End If
    
For i = 2 To dividendscount + 1
    searchsymbol = Sheet5.Cells(i, 3)
    
stSQL = "SELECT DISTINCT Stuff FROM Tables WHERE LinkTransactions.LinkSymbol='" & searchsymbol & "';"

    With Sheet2.ListObjects.Add(SourceType:=0, Source:=Array( _
        "OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=.\portfoliocenter;Use Procedure for Prepare=" _
        , _
        "1;Auto Translate=True;Packet Size=4096;Workstation ID=LAPTOP;Use Encryption for Data=False;Tag with column collation when possib" _
        , "le=False;Initial Catalog=PortfolioCenter"), Destination:=Sheet2.Range("$A" & (2 * i) - 3)). _
        QueryTable
        .CommandText = stSQL

        .Refresh BackgroundQuery:=False
    End With
    
nexti:
Next i
But when I run the above code, Excel repeats the header row for the SQL query every time. How can I stop this from happening so that I can just have the data displayed neatly, row after row?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top