我有一个“大”SQL查询(如200行)......
dim query as string
query = "..................................." & _
"..................................." & _
.... Like a lot lines later...
"..................................."
function query,"sheet 1"
当我这样做时,Excel会说“线路延续太多。”
避免这种情况的最佳方法是什么?
答案 0 :(得分:10)
只有一种方法 - 使用较少的延续。
这可以通过在一行上添加更多文本或使用以不同方式表达的串联来完成:
query = ".........."
query = query & ".........."
query = query & ".........."
但最好是从外部来源加载文本,作为一个整体。
答案 1 :(得分:1)
到目前为止,我发现了这个......
Call AddToArray(query, "...")
Call AddToArray(query, "...")
... a lot lines later...
Call AddToArray(query, "...")
*编辑:忘记添加:
Sub AddToArray(myArray As Variant, arrayElement As Variant)
If Not IsArrayInitialized(myArray) Then
ReDim myArray(0)
myArray(0) = arrayElement
Else
ReDim Preserve myArray(UBound(myArray) + 1)
myArray(UBound(myArray)) = arrayElement
End If
End Sub
来源:link text X(谢谢你
(还在等待更好的方法......)谢谢你:P
答案 2 :(得分:1)
将查询拆分为几个部分:
query = _
"............" & _
"............" & _
"............"
query = query & _
"............" & _
"............" & _
"............"
query = query & _
"............" & _
"............" & _
"............"
答案 3 :(得分:0)
为什么不使用VBA来帮助VBA串联?
检查我的这段代码(它非常原始,可以随意调整),它基本上会获取工作表上“ A:E”列中名为“ Fast_string”的任何数据,而在F列中它将为您准备代码使用初步变量“ prelim_string”进行串联。您只需要使用它,然后将解决方案从F列复制粘贴到您的代码中,欢迎您;)
Sub FAST_STRING()
Dim cel As Range, lastcel As Range, prel_r As String, i As Integer, cr As Integer
With ThisWorkbook.Worksheets("Fast_string")
Set lastcel = .Cells(10000, 1).End(xlUp)
For Each cel In .Range(.Cells(1, 1), lastcel)
cr = cel.row
prel_r = ""
For i = 1 To 5
If .Cells(cr, i) = "" Then
prel_r = prel_r & " "
Else
prel_r = prel_r & " " & Replace(.Cells(cr, i).Value, """", """""")
End If
Next i
If cr = 1 Then
prel_r = "Prelim_string =" & """" & prel_r & """" & " & chr(10) & _"
ElseIf cr / 20 = Round(cr / 20) Then
prel_r = "Prelim_string = Prelim_string & " & """" & prel_r & """" & " & chr(10) & _"
Else
prel_r = """" & prel_r & """" & " & chr(10) & _"
End If
If cr = lastcel.row Or (cr + 1) / 20 = Round((cr + 1) / 20) Then prel_r = Left(prel_r, Len(prel_r) - 14)
cel(1, 6) = prel_r
Next cel
End With
End Sub