发布网友 发布时间:2022-04-27 09:13
共1个回答
热心网友 时间:2023-08-30 02:41
EXCEL里使用SQL,這是自定義的過程,你在控件的事件下面調用他就可以了. 使用ADODB要先設定. VBA菜單->工具->設定(microsoft activeX data objectS 2.0 library) SQL字符串,表名等可以通過inputbox()函數和其他方式輸入. 此過程會把查詢的結果放到sheet1. Sub mxbing() Dim cnn As ADODB.Connection Dim rs As ADODB.Recordset Set cnn = New ADODB.Connection With Sheet1 .Cells.Clear With cnn .Provider = "microsoft.jet.oledb.4.0" .ConnectionString = "Extended Properties=Excel 5.0;" + "Data Source="表名.xls" .Open End With Set rs = New ADODB.Recordset Sql = "select * From [Sheet1$] where 條件 order by 字段" rs.Open Sql, cnn, adOpenKeyset, adLockBatchOptimistic R = .Range("A65536").End(xlUp).Row - 1 .Range("A" & R + 1).CopyFromRecordset rs End With rs.Close cnn.Close Set rs = Nothing Set cnn = Nothing End Sub