I am extracting sales data from SQL table and populate it into "Table1" in "Sheet1" per below code below:
Sub SalesPerCountry()
Dim NewRow As ListRow
Set Table = Sheets("Sheet1").ListObjects("Table1")
' Set Connection and run
Set Connection = CreateObject("ADODB.Connection")
Connection.Open "Provider=###;Password=###;User ID=### ;Data Source=###
Set SQL1 = Connection.Execute("Select Country, Sum(Sales) as Sales From [Market_Sales]
Group by Country")
' Delete data from table
If Not Table.DataBodyRange Is Nothing Then Table.DataBodyRange.Delete
' Populate data
r = 0
While Not SQL1.EOF
r = r + 1
Set NewRow = Table.ListRows.Add(r)
NewRow.Range(1) = SQL1("Country").Value
NewRow.Range(2) = SQL1("Sales").Value
SQL1.MoveNext
Wend
End Sub
I need to make the code more efficient as table is very big now and macro takes minutes to run.
I want to be able to push all data at once into the excel table, something like below:
Worksheets("Sheet1").ListObjects("Table1").DataBodyRange.Value = SQL1
but this doesn't run
any idea how to push the SQL1 object data into the table?
Thanks