Use an Excel Cell to Pass Multiple Departments to a SQL IN Query
Question details
Use multiple department values stored in an Excel cell to filter a SQL query.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- An Excel VBA query needs to filter records by several departments listed in a worksheet cell.
- Observed behavior
- A single parameter placeholder accepts one value, but a SQL IN clause needs a separate parameter or SQL literal for each department.
Check that your Excel workbook already has a working database connection and identify the cell containing the department list. Confirm whether your database provider supports parameters and which placeholder syntax it uses.
Use a separate SQL parameter for each department
Use this approach when department names may come from users or another untrusted source.
A single parameter represents one value, not a comma-separated list of SQL values. Create one placeholder and one parameter for each department.
In cell A1, enter department names separated by commas, such as Sales,HR,Finance. This simple format assumes department names do not contain commas.
Open Developer > Visual Basic and edit the VBA procedure that runs your query. Read Range("A1").Value, split it on commas, and trim each value. Reject an empty list or empty entries before building the query.
Build the IN clause with one placeholder per department. For a provider that supports question-mark placeholders, three values would use: SELECT * FROM aa WHERE dept IN (?, ?, ?).
Use your database command object's Parameters collection to add each department as a separate text parameter, in placeholder order, then execute the command. Use the parameter syntax and data type required by your provider.

Build the SQL IN clause from a trusted, formatted cell
Use this approach only when the cell contains a trusted SQL-formatted list and your query is designed to concatenate it.
Work with Department Lists in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and work with department lists as you migrate; an existing Excel VBA database query may need separate compatibility checks or changes.

Frequently Asked Questions
Why doesn't one parameter work for a comma-separated IN list?
A parameter is treated as one value, so Sales,HR,Finance is not automatically expanded into three SQL values. Use a separate placeholder and parameter for each department.
How should I handle an apostrophe in a department name?
With separate parameters, pass the department name as data. If you are constructing a trusted SQL literal, double the apostrophe, for example 'O''Brien'.
What should I do if the department cell is empty?
Check A1 before executing the query and prompt for a department or apply another explicitly chosen behavior. Do not generate an empty IN clause without checking your database's syntax.
Can I use a temporary table instead of an IN list?
Yes, if your database supports temporary tables. Insert the department values using parameters, then join or filter against the table. Ensure the query can access the temporary table, often by using the same connection.




