|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Left使用SQL语句,结果成立。
Str = "Select Left(地点,22) + '.JPG',Count(大小),Count(数量) from [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Group by Left(地点,22) "
replace用在SQL中,结果出错。
Str = "Select replace(地点,'(1)',''),Count(大小),Count(数量) from [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Group by 地点 "
- Function SqlRetuRs(Str)
- Dim Cn As ADODB.Connection
- Set Cn = New ADODB.Connection
- Dim Rs As ADODB.Recordset
- Set Rs = New ADODB.Recordset
- Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
- Rs.Open Str, Cn, adOpenKeyset, adLockOptimistic
- Set SqlRetuRs = Rs
- End Function
- '''
- Sub dell()
- Dim Sht As Worksheet
- Set Sht = Sheet1
- Dim Rng As Range
- Set Rng = Sht.Range("A1:D" & Sht.Cells(65536, 1).End(xlUp).Row + 10)
- Debug.Print Rng.Address
- Dim Rs As Recordset, Str
-
- Str = "Select 地点,Count(大小),Count(数量) from [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Group by 地点 "
- Str = "Select replace(地点,'(1)',''),Count(大小),Count(数量) from [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Group by 地点 "
- 'Str = "Select Left(地点,22) + '.JPG',Count(大小),Count(数量) from [" & Sht.Name & "$" & Rng.Address(0, 0) & "] Group by Left(地点,22) "
- ''
- Debug.Print Str
- Set Rs = SqlRetuRs(Str)
- Debug.Print Rs.RecordCount
- With Sheet1
- .Cells.Font.Size = 9
- .Cells(2, "F").CopyFromRecordset Rs
- End With
- End Sub
复制代码
|
|