代码如下(摘自——看见星光) Sub DoSql_Execute1() Dim cnn As Object, rst As Object Dim strPath As String, str_cnn As String, strSQL As String Dim i As Long Set cnn = CreateObject("adodb.connection") strPath = ThisWorkbook.FullName If Application.Version < 12 Then str_cnn = "Provider=Microsoft.jet.OLEDB.4.0;ExtendedProperties=Excel 8.0;Data Source=" & strPath Else str_cnn = "Provider=Microsoft.ACE.OLEDB.12.0;ExtendedProperties=Excel 12.0;Data Source=" & strPath End If cnn.Open str_cnn strSQL = "SELECT 姓名,成绩 FROM [Sheet1$] WHERE 成绩>=80" Set rst = cnn.Execute(strSQL) Worksheets("结果表").Select Cells.ClearContents For i = 0 To rst.Fields.Count - 1 Cells(1, i + 1) = rst.Fields(i).Name Next Range("a2").CopyFromRecordset rst cnn.Close Set cnn = Nothing End Sub 代码运行后str_cnn ="Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;DataSource=" & strPath 但往往在【cnn.Open str_cnn】这一句中出错。
|