SQL in VBA use values in a worksheet?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello,

Hope someone can help.

I have a SQL view that I am calling from VBA. Something basic like
Code:
SELECT * FROM TheWennerWomans_View

The view contains my qualifiers (so, for example, it contains a field called NominalCode) which has about forty values.

Instead of NominalCode being stored in the view, I've been asked to store it on a separate worksheet so that the users can add / delete nominal codes as necessary.

So my SQL code would now need to be something like
Code:
SELECT * FROM TheWennerWomans_View WHERE NominalCode IN (SELECT NominalCode FROM Sheet1)

Has anyone done anything similar? Hope this makes sense :)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You'd need a function to build up a list of the values from the range. Are the codes text or numbers in your database?
 
Upvote 0
You can use a function like this:

Code:
Function GetStringInList(CriteriaRange As Range, Optional TextDelim As String = "") As String
   Dim Cell As Range
   For Each Cell In CriteriaRange.Cells
      Dim loopText As String
      If Len(Cell.Value) > 0 Then loopText = loopText & "," & TextDelim & Replace$(Cell.Value, "'", "''") & TextDelim
   Next Cell
   ' now strip off leading comma
   GetStringInList = Mid$(loopText, 2)
End Function

If the values are numeric, you'd just use getstringinlist(range("A1:A10")) or if textual, use getstringinlist(range("A1:A10"), "'") for example.
 
Upvote 0
You'd build up your SQL string using code like:

Code:
SQLString = "SELECT * FROM TheWennerWomans_View WHERE NominalCode IN (" & getstringinlist(range("A1:A10"), "'") & ")"

for example.
 
Upvote 0
Solution
You'd build up your SQL string using code like:

Code:
SQLString = "SELECT * FROM TheWennerWomans_View WHERE NominalCode IN (" & getstringinlist(range("A1:A10"), "'") & ")"

for example.
Very good, thank you very much
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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