简体   繁体   中英

Run SQL-statement in Excel Cell with VBA

Excel-File:

   |        A       |       B         |            C                |        D        |     E
---|----------------|-----------------|-----------------------------|-----------------|------------
1  |   sales_date   |   2020-01-15    |     =CONCATENATE(C2,C3)     |                 |
2  |                |                 |      SQL-Query Part 1*      |                 |
3  |                |                 |      SQL-Query Part 2*      |                 |
4  |                |                 |                             |                 |

VBA

Sub Get_Data_from_DWH()

    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
        
    Set conn = New ADODB.Connection
    conn.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=XX.XXX.XXX.XX; DATABASE=bi; UID=testuser; PWD=test; OPTION=3"
    conn.Open
    
    strSQL = Sheet1.Range("C1")
                            
    Set rs = New ADODB.Recordset
    rs.Open strSQL, conn, adOpenStatic

    Sheet1.Range("D1").CopyFromRecordset rs
    
    rs.Close
    conn.Close
    
End Sub

SQL-Query:

SELECT
product, brand, sales_channel,
country, sales_manager, sales_date, return_date,
process_type, sale_quantity, return_quantity, sales_value, variable_costs, fixed_costs
FROM bi.sales
WHERE sales_date = '"&B1&" AND country IN ('DE', 'US', 'NL') 
ORDER BY FIELD (brand, 'brand_A', 'brand_B', 'brand_C');

I want to run the above SQL-Query with the above VBA .

The problem is that the query contains more than 255 characters so it does not fit in one Excel-Cell.
Therefore, I randomly split the query into two parts in Cell C2 and Cell C3 and used CONCATENATE(C2,C3) in Cell C1 to combine both parts of the query.

When I now run the VBA I get runtime error '-2147217887 (80040e21)' .


I guess that VBA cannot handle the CONCATENATE(C2,C3) in Cell C1 .
Therefore, I am wondering if there is any other way to solve this issue?


NOTE:
I know I could put the SQL directly into the VBA code. However, my idea is to split the SLQ-string and the execution code since my origianl SQL has even more characters than the example in this quesion and I want to keep the VBA structured as described in this question .

From where do you get the notion that an excel cell is limited to 265 characters?

Excel 365: This is 313 characters in C1: 在此处输入图像描述

This is the whole SQL query in cell A1: 在此处输入图像描述

Option 1:

Splitting the query into parts using CONCATENATE function can delete empty spaces which are needed to run the query correctly.
Therefore, depending on the query instead of =CONCATENATE(C2,C3) you need to use =CONCATENATE(C2," ",C3) .


Option 2:

If the above solution does not work you can also solve the issue by doing a work-around and copy&paste the CONCATENATE function from Cell C1 into another cell as Values :

Sub Get_Data_from_DWH()

Sheet1.Range("C4").ClearContents
Sheet1.Range("C1").Copy
Sheet1.Range("C4").Cells.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
        
    Set conn = New ADODB.Connection
    conn.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=XX.XXX.XXX.XX; DATABASE=bi; UID=testuser; PWD=test; OPTION=3"
    conn.Open
    
    strSQL = Sheet1.Range("C4")
                            
    Set rs = New ADODB.Recordset
    rs.Open strSQL, conn, adOpenStatic

    Sheet1.Range("D1").CopyFromRecordset rs
    
    rs.Close
    conn.Close
    
End Sub

In the comments it was discussed that there is no limitation on an Excel cell.
For a simple text there is no limitation but the entry of a formula is limited.
In the specific case in the question a formula is necessary because the entry in Cell B1 should be a variable that can be changed by the user anytime.


Therefore, I think the only way to

a) seperate the SQL-statement from the executing VBA-Code
b) use a query with more than 255 characters
c) apply entries in an Excel cell as variables

is one of the above solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM