VBA连接字符串

时间:2018-01-23 13:28:30

标签: sql excel vba concatenation

我有一些VBA代码将各种单元格(总是连续地)串在一起,当它们放在一起时创建一个SQL查询(最终用户有下拉菜单来创建动态报告)。这不是理想的,因为我必须列出所有单元格以及我得到的VBA错误太长。见下面的示例

Sub Custom_Query

Dim strsql As String

If Worksheets("Animal_Entry").Range("B1") = "Dog" And Worksheets("Animal_Entry").Range("E1") = "Food_Consumption" Then

strsql = Worksheets("Custom_Queries").Range("A362")
strsql = strsql & vbNewLine
strsql = strsql & Worksheets("Custom_Queries").Range("A363")
strsql = strsql & vbNewLine
strsql = strsql & Worksheets("Custom_Queries").Range("A364")
strsql = strsql & vbNewLine
strsql = strsql & Worksheets("Custom_Queries").Range("A365")
strsql = strsql & vbNewLine
strsql = strsql & Worksheets("Custom_Queries").Range("A366")

With ActiveWorkbook.Connections("Custom_Query").ODBCConnection
    .BackgroundQuery = True
     Debug.Print strsql
    .CommandText = strsql

    End With

ActiveWorkbook.Connections("Custom_Query").Refresh

ElseIf Worksheets("Animal_Entry").Range("B1") = "Dog" And Worksheets("Animal_Entry").Range("E1") = "Bathroom_Breaks" Then

strsql = Worksheets("Custom_Queries").Range("A372")
strsql = strsql & vbNewLine
strsql = strsql & Worksheets("Custom_Queries").Range("A373")
strsql = strsql & vbNewLine
strsql = strsql & Worksheets("Custom_Queries").Range("A374")
strsql = strsql & vbNewLine
strsql = strsql & Worksheets("Custom_Queries").Range("A375")
strsql = strsql & vbNewLine
strsql = strsql & Worksheets("Custom_Queries").Range("A376")

With ActiveWorkbook.Connections("Custom_Query").ODBCConnection
    .BackgroundQuery = True
     Debug.Print strsql
    .CommandText = strsql

    End With

ActiveWorkbook.Connections("Custom_Query").Refresh

End if

End Sub

我想使用以下内容。但是,我无法弄清楚它的工作原理。

Sub Custom_Query()

Dim strsql As String

If Worksheets("Animal_Entry").Range("B1") = "Dog" And Worksheets("Animal_Entry").Range("E1") = "Food_Consumption" Then

    For i = 362 To 366
        strsql = Worksheets("Custom_Queries").Range("A" & i) & vbNewLine
    Next i

With ActiveWorkbook.Connections("Custom_Query").ODBCConnection
    .BackgroundQuery = True
     Debug.Print strsql
    .CommandText = strsql

    End With

ActiveWorkbook.Connections("Custom_Query").Refresh

ElseIf Worksheets("Animal_Entry").Range("B1") = "Dog" And Worksheets("Animal_Entry").Range("E1") = "Bathroom_Breaks" Then

    For i = 372 To 376
        strsql = Worksheets("Custom_Queries").Range("A" & i) & vbNewLine
    Next i

With ActiveWorkbook.Connections("Custom_Query").ODBCConnection
    .BackgroundQuery = True
     Debug.Print strsql
    .CommandText = strsql

    End With

ActiveWorkbook.Connections("Custom_Query").Refresh

End if

End Sub

非常感谢任何有关正确方向的提示!

1 个答案:

答案 0 :(得分:0)

所以我无法找到与我想要在我的OP中做的相匹配的解决方案,但我确实创造了一个解决方案。我没有在一个大的VBA部分中创建动态SQL语句,而是在自己的VBA部分创建了每个SQL查询,然后在主查询中调用它。

Sub custom_Query()

If Worksheets("Animal_Entry").Range("Selection") = "Dog" And Worksheets("Animal_Entry").Range("E1") = "Food_Consumption" Then

Call Food_Consumption

ElseIf Worksheets("Animal_Entry").Range("Selection") = "Dog" And Worksheets("Animal_Entry").Range("E1") = "Bathroom Breaks" Then

Call Bathroom_Break

end IF
End Sub