|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub myCount()
- Dim lngRows As Long
- Dim Conn As Object, Rst As Object, strPath As String
- Dim strConn As String, strSQL As String
- Dim rg As Range
- '清空结果区域
- lngRows = Sheet1.Range("K" & Rows.Count).End(xlUp).Row
- Sheet1.Range("K2:M" & lngRows).ClearContents
- lngRows = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
- Set rg = Sheet1.Range("K2")
- Set Conn = CreateObject("ADODB.Connection")
- Set Rst = CreateObject("ADODB.Recordset")
- strPath = ThisWorkbook.FullName '设置工作簿的完整路径和名称
- Select Case Application.Version * 1 '设置连接字符串,根据版本创建连接
- Case Is <= 11
- strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & strPath
- Case Is >= 12
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
- End Select
- Conn.Open strConn '打开数据库链接
- '''''''''''''''''''''''''''''''''
- strSQL = "SELECT 甲,乙,丙 " & _
- "FROM [Sheet1$A1:I" & lngRows & "] " & _
- "Group By 甲,乙,丙"
- Rst.Open strSQL, Conn, 3, 1 '执行查询,并将结果输出到记录集对象
- rg.CopyFromRecordset Rst
- Set Rst = Nothing
- Set Conn = Nothing
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|