|
楼主 |
发表于 2024-6-3 09:11
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
zpy2 发表于 2024-6-1 12:08
'Str = "Select Name From [Sheet1$C1:C14] Order by Name"
Str = "Select Name,count(Name) From [ ...
Sub ll()
Dim Str, Str1, SqlStr
Dim Rs As Recordset
Dim Rng As Range
Set Rng = Selection
Dim Sht As Worksheet
Set Sht = Rng.Parent
Debug.Print Sht.Cells(1, 1).Formula
Set Rng = Sht.Range(Sht.Cells(1, 1).Formula)
Str = "Select Name,Count(Name) From [" & Sht.Name & "$" & Rng.Address(0, 0) & "] " ' where Not Name is Null"
Debug.Print Str
Str1 = "Select Name,Count(Name) From [Sheet1$C1:C14] Group By Name "
Str = "Select Name,(" & Str1 & ") From [Sheet1$C1:C14] "
Str = "Select Name,Count(Name) From [Sheet1$C1:C18] Group By Name Where Not Name Is Null"
Str = "Select Name,Count(Name) From [Sheet1$C1:C18] Group By Name"
Debug.Print Str
Set Rs = SqlRetuRs(Str)
'
Set Rng = Sht.Cells(3, 8)
Rng.CopyFromRecordset Rs
Stop
End Sub
Function SqlRetuRs(SqlStr)
Dim Cn As ADODB.Connection
Set Cn = New ADODB.Connection
Dim Rs As ADODB.Recordset
Set Rs = New ADODB.Recordset
'
If InStr(UCase(Application.Path), "WPS") > 0 Then
Cn.Open "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=yes';Data Source=" & ThisWorkbook.FullName
Else
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
End If
Rs.Open SqlStr, Cn, adOpenKeyset, adLockOptimistic
Set SqlRetuRs = Rs
End Function
|
|