Macro to pull specific SQL data based on Cell Range

GATA_Spread

New Member
Joined
Aug 3, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Looking to create a macro that will run a SQL query based on a range of cells in Column A and return the data back into the worksheet. The sheet will be used by multiple users, and the values in column A will change based on the user. Sometimes there may be 100 items Column A, sometimes only 2. The SQL DB has over 18 million rows, so it is too large to bring directly into the worksheet. I have googled this, but have been unable to find a solution.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Thanks, but I don't see where it shows how to reference a range. Here is the code I've tried without success:

VBA Code:
Private Sub SKU_Tracker()



Dim cn As ADODB.Connection

Set cn = New ADODB.Connection



Dim strConn As String



strConn = "Provider=SQLOLEDB;"



strConn = strConn & "server=<server name>;INITIAL CATALOG=<DB Name>;"



strConn = strConn & " INTEGRATED SECURITY=sspi;"



cn.Open strConn





ActiveSheet.Range("B2:F100").Clear

Dim SKUNumber As String



SKUNumber = Range("A2:A100").Value



Dim rs As ADODB.Recordset

Set rs = New ADODB.Recordset



SQLStr = "Select ONLINE_SKU, MODEL, BRAND, VENDOR, from <table name> WHERE RETAIL_SKU = " & ItemNumber & ""



rs.Open SQLStr, cn



rs.Close

cn.Close

Set rs = Nothing

Set cn = Nothing
 
Upvote 0
I think you will need to build the where string to include you list of up to 100 items

example:

VBA Code:
......
items =""
for r = 1 to Cells(Rows.Count, "A").End(xlUp).Row
   items=items & cells(r,"A") & "," 'build the list
next r

ItemNumber="in (" & items & ")" ' this will create the list       example:  "in (123,456,789,abc,def)"

SQLStr = "Select ONLINE_SKU, MODEL, BRAND, VENDOR, from <table name> WHERE RETAIL_SKU = " & ItemNumber & ""
 
Upvote 0

Forum statistics

Threads
1,215,261
Messages
6,123,932
Members
449,134
Latest member
NickWBA

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