请查看我的以下VBA代码。
Function SQL_query(ByRef sql_string As Variant)
Application.ScreenUpdating = False
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim strCon As String
strFile = ThisWorkbook.FullName
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Set cn = New ADODB.Connection
Set rs1 = New ADODB.Recordset
cn.Open strCon
strSQL = sql_string
rs1.Open strSQL, cn, adOpenForwardOnly, adLockReadOnly
我使用以下2种方法来粘贴数据。
第一种方法: -
i = 2
Do While Not rs1.EOF
If rs1.Fields(0) <> "" Then
Cells(i, 7) = rs1.Fields(0)
End If
rs1.MoveNext
i = i + 1
Loop
第二种方法: -
Sheet5.Range("G2").CopyFromRecordset rs1
剩余代码
rs1.Close
cn.Close
Application.ScreenUpdating = True
End Function
这两种方法都需要30-40分钟来粘贴9,000行,这是一个非常耗时的过程。请指导如何最大限度地缩短时间。
请找到SQL查询字符串 -
slt = "CStr([Sheet5$].[ClientCode])"
sql_string = "Select " & _
" CStr([one_key$].[APRIL 2017]) From [Sheet5$]" & _
" LEFT JOIN [one_key$] On CStr([Sheet5$].[ClientCode]) = CStr(Mid([one_key$].[Client],InStr(1,[one_key$].[Client],""#"")+1))"