|
楼主 |
发表于 2023-4-13 06:28
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Set Rs = Cn.Execute(SqlStr) 结果Rs.recordcount=-1 不是目标需求
Arr = Rs.GetRows,目标需求的结果
Rng.CopyFromRecordset Rs , 记录集是空集,Rng没有结果。
- Sub ll1()
- Dim Cn As ADODB.Connection
- Dim Rs As ADODB.Recordset
- Dim SqlStr, Arr
- Set Cn = New ADODB.Connection
- Set Rs = New ADODB.Recordset
- Set Cn = New ADODB.Connection
- Set Rs = New ADODB.Recordset
- Cn.Open "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
- SqlStr = "Select Distinct * From [d:\ls.xls].[Sheet1$A5:c10]"
- Set Rs = Cn.Execute(SqlStr)
-
- Debug.Print Rs.RecordCount, ThisWorkbook.FullName
- Arr = Rs.GetRows
- Dim Rng As Range
- Set Rng = Sheet1.Cells(1, "D") '.Resize(10, 20)
- Rng.CopyFromRecordset Rs ', 2, 2
- Debug.Print Rng.Address,
- Debug.Print Rng.Rows.Count, Rng.Columns.Count,
- Debug.Print Rng(1, 1), Rng(2, 1),
- Debug.Print UBound(Arr), Arr(2, 2)
-
- Stop
-
- Stop
- End Sub
复制代码
|
|