I want to get data from an Excel worksheet in an SQL-like mode. Meaning, I have following columns: code, type, name, product_code, price, quantity.
What I want to do is get the data from this sheet like it was a db table and I execute the following:
select code, type, name, product_code, sum(price), sum(quantity) from table
group by code, type, name, product_code
How can I do this in VBA?
Order your data by
code, type, name, product_code, then write a loop that iterates through every row on the sheet, aggregating as it goes. When any of those four fields changes value, you’re in a new group, so write out your current aggregate and start again on the current row.There is not, to my knowledge, any way to write SQL against an Excel sheet in VBA; you just write the loop yourself.
Personally, I’d avoid VBA and use a pivot table.