Data Import & Export

Use an Excel Cell to Pass Multiple Departments to a SQL IN Query

Phi Hung VoPhi Hung Vo Nov 18, 2026 868 views

Question details

Use multiple department values stored in an Excel cell to filter a SQL query.

Use an Excel Cell to Pass Multiple Departments to a SQL IN 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.
Before you start

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.

Solution 1Recommended

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.

1
Enter the departments

In cell A1, enter department names separated by commas, such as Sales,HR,Finance. This simple format assumes department names do not contain commas.

2
Split the cell value

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.

3
Create placeholders

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 (?, ?, ?).

4
Bind and execute

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.

Use a separate SQL parameter for each department
Provider support varies: Some providers use named parameters rather than question marks. For large lists, a temporary table may be more appropriate if your database supports it.
Free Microsoft Office alternative

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.

Organize and filter department lists in WPS SpreadsheetsOpen and save compatible Microsoft Excel XLSX filesUse a familiar interface for everyday spreadsheet workMove existing spreadsheets into a lightweight office suite
microsoft office alternative - wps office

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.