ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   SQL Query from Excel (https://www.excelbanter.com/excel-programming/299684-re-sql-query-excel.html)

Juan Pablo González

SQL Query from Excel
 
Sure, you could do that. Assuming that the data is pasted in Sheet1,
starting in A2 and below:

Sub MakeSQL()
Dim i As Long
Dim Sql As String
With Sheets("Sheet1")
For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
Sql = Sql & ",'" & .Cells(i, 1).Value & "'"
Next i
Sql = Mid$(Sql, 2)
End With
Sql = "select month, sum(sales_amount) from CompanySales where
Company_Name in (" Sql & ") group by month;"
End Sub

I assumed that you have to enclose the texts within single quotes, as
usual...

--
Regards

Juan Pablo González

"Rob" wrote in message
...
I have a need for our business customers to be able to insert a list of

customers then chart their performance. The data is stored in SQL Server.
I would like for them to be able to paste a list of customers into the
spreadsheet and then use that list in my database query. There are about
8000 customers and most of the lists would have about 100 of the customers.

An example would be them pasting a list like this into Sheet 1:
company ABC
company 123
Company XYZ

I would want my query to look like this:
select month, sum(sales_amount) from CompanySales where Company_Name in

(company ABC, company 123, Company XYZ) group by month;

Then I could create an Excel chart on the returned data. Is there any way

to do this or would this have to be done with some kind of filter?

Thanks,
Rob





All times are GMT +1. The time now is 01:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com