Good questions, I'll try to clarify:
1. My raw data has state-level information. I created a reference table with state/region that I link to so I can sum my data by region (you won't see this table in my SQL code because my output query is querying other queries that have already summed the data grouped by region - if I had to redo it, I would do this all in one query with subqueries, but this has been a progression with new requirements, so it's a little messy).
2. My output query gives me 30 rows of data, 3 rows for each region. I want to export one region's data automatically to a specified sheet and range in Excel. My previous code does this, but I'd like to not have to write queries for each region. For one project, I would have to write 10 queries instead of 1 parameter query, and there are multiple projects, so this is a magnitude of 10 for each project and/or report which could get quite ridiculous. Instead I'd like to leverage the ability to use parameter queries and pass the value to it (region) in my VBA loop using case statements. I think I can also apply this approach to future logic which will save tons of time and reduce potential errors.
The only part I don't have figured out is how to pass a string variable to a parameter query in VBA inside a loop and shoot that data out to Excel (also inside the loop so that my variable/query can run for every region).
Let me know if you have any more questions. Thanks a ton!
SQL:
SELECT
[qry_counts].[Region],
[qry_rawsum].[Project],
[qry_allocation].[Allocation],
[qry_rawsum].[Amount],
[qry_rawsum].[Draw Downs],
[qry_rawsum].[Balance],
[qry_rawsum].[Holds],
[qry_rawsum].[Available],
[qry_counts].[Total Count],
[qry_counts].[Hold Count],
IIf([Amount]=0,0,[Draw Downs]/[Amount]) AS [Pct of Draw Downs],
IIf([Amount]=0,0,[Balance]/[Amount]) AS [Pct of Balance],
IIf([Amount]=0,0,[Holds]/[Amount]) AS [Pct of Holds],
IIf([Amount]=0,0,[Available]/[Amount]) AS [Pct of Available],
IIf([Balance]=0,0,[Holds]/[Balance]) AS [Pct of Balance on Hold],
IIf([Balance]=0,0,[Available]/[Balance]) AS [Pct of Balance Available]
FROM
[qry_allocation]
INNER JOIN
(
[qry_counts]
INNER JOIN
[qry_rawsum]
ON
([qry_counts].[Project] = [qry_rawsum].[Project]) AND
([qry_counts].[Region] = [qry_rawsum].[Region]))
ON
([qry_allocation].[Project] = [qry_counts].[Project]) AND
([qry_allocation].[Region] = [qry_counts].[Region]);