1

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?

1
  • when you say "group by code, type, name, product name", you mean have all the rows sorted by code and then sorted by type and name and product code? Also, what are the columns (ie. is code in column A and so on?). What's sum(price) and sum(quantity)?
    – frenchie
    Feb 10, 2012 at 10:35

3 Answers 3

5

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.

1
1

You can query the worksheet using Access SQL e.g. using ADO classic. However, because the data types are determined at run-time using the current state of the data and registry keys (which may result in values coerced to the wrong type and/or nulls where values cannot be coerced), it is perhaps better to import the data into a table in a SQL database so that data types can be specified at design-time, for which SQL Server is well suited.

1

SQLite for excel will let you do it in-memory. I'm not exactly sure what-all LINQ provides to the .NET crowd, but it seems you can emulate some of it using SQLite. I used it just enough to verify it works, but haven't gotten around to building a utility class around it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.