简体   繁体   English

使用 VBA 在 Excel 单元中运行 SQL 语句

[英]Run SQL-statement in Excel Cell with VBA

Excel-File: Excel文件:

   |        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 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: SQL查询:

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 .我想用上面的VBA运行上面的SQL-Query

The problem is that the query contains more than 255 characters so it does not fit in one Excel-Cell.问题是查询包含超过 255 个字符,因此它不适合一个 Excel 单元格。
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.因此,我在Cell C2Cell C3中将查询随机拆分为两部分,并在Cell C1中使用CONCATENATE(C2,C3)来组合查询的两个部分。

When I now run the VBA I get runtime error '-2147217887 (80040e21)' .当我现在运行VBA时, runtime error '-2147217887 (80040e21)'


I guess that VBA cannot handle the CONCATENATE(C2,C3) in Cell C1 .我猜VBA无法处理Cell C1中的CONCATENATE(C2,C3)
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.我知道我可以将 SQL 直接放入 VBA 代码中。 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 .但是,我的想法是拆分 SLQ 字符串和执行代码,因为我的原始 SQL 具有比此问题中的示例更多的字符,我想保持 VBA 的结构如本问题中所述。

From where do you get the notion that an excel cell is limited to 265 characters?您从哪里得知 excel 单元限制为 265 个字符的概念?

Excel 365: This is 313 characters in C1: Excel 365:这是 C1 中的 313 个字符: 在此处输入图像描述

This is the whole SQL query in cell A1:这是单元格 A1 中的整个 SQL 查询: 在此处输入图像描述

Option 1:选项1:

Splitting the query into parts using CONCATENATE function can delete empty spaces which are needed to run the query correctly.使用CONCATENATE function 将查询拆分为多个部分可以删除正确运行查询所需的空白。
Therefore, depending on the query instead of =CONCATENATE(C2,C3) you need to use =CONCATENATE(C2," ",C3) .因此,根据查询而不是=CONCATENATE(C2,C3)您需要使用=CONCATENATE(C2," ",C3)


Option 2:选项 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 :如果上述解决方案不起作用,您也可以通过变通方法解决问题,并将Cell C1中的CONCATENATE function 作为复制并粘贴到另一个单元格中:

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.在评论中讨论了对 Excel 单元没有限制。
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.在问题的特定情况下,公式是必要的,因为Cell B1中的条目应该是用户可以随时更改的变量。


Therefore, I think the only way to因此,我认为唯一的方法是

a) seperate the SQL-statement from the executing VBA-Code a) 将 SQL 语句与执行的 VBA 代码分开
b) use a query with more than 255 characters b) 使用超过 255 个字符的查询
c) apply entries in an Excel cell as variables c) 将 Excel 单元格中的条目作为变量应用

is one of the above solutions.是上述解决方案之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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