我用vba写了一段程序引用数据库中的内容,但是运行速度特别慢,要半小时左右,请大家看下如何提高效率,谢谢
Sub OpenSQLConnection_Clm_Cnt()
Application.ScreenUpdating = False
Dim cn As XXXX.Connection
Dim SQL As String
Set cn = XXXX.Connection
cn.ConnectionTimeout = 30000
cn.CommandTimeout = 9000
'cn.Open "Provider=XXXX;DSN=XXXX"
cn.Open "Driver={SQL Server};" & _
"Server=XXXX;" & _
"Database=XXX; Trusted_Connection=yes;"
Dim rsPubs As XXXX.Recordset
Set rsPubs = XXXX.Recordset
With rsPubs
' Assign the Connection object.
.ActiveConnection = cn
' Extract the required records.
SQL = "select ...."
SQL = SQL & " from claims_detail_all"
SQL = SQL & " where ..."
SQL = SQL & " and ..."
SQL = SQL & " group by ...,
SQL = SQL & " order by ........
.Open SQL
' Copy the records into cell B2 on Sheet2.
Sheet2.Range("B3", "I50000").ClearContents
Sheet2.Range("B3").CopyFromRecordset rsPubs
End With
Application.ScreenUpdating = True
End Sub