|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
如果文件名长需要用[文件名含扩展名]
Sub a()
Dim cnn As Object, SQL$, s$, p$, f$
Set cnn = CreateObject("ADODB.Connection")
p = ThisWorkbook.Path & "\"
f = Dir(p & "*.CSV")
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='text;FMT=Delimited(,);hdr=YES';Data Source=" & p
Do While f <> ""
SQL = SQL & " SELECT INT(交易日期) AS T,还款本金,还款利息,还款总金额,交易手续费 FROM [" & f & "] UNION ALL "
f = Dir()
Loop
SQL = Left(SQL, Len(SQL) - 11)
SQL = "SELECT T,SUM(还款本金),SUM(还款利息),SUM(还款总金额),SUM(交易手续费) FROM (" & SQL & ") GROUP BY T"
[A2:E999] = ""
[a2].CopyFromRecordset cnn.Execute(SQL)
cnn.Close
Set cnn = Nothing
Dim R%
R = [A999].End(3).Row
[a1].Offset(R, 0) = "合计"
[b1].Offset(R, 0).Resize(1, 4) = "=sum(b2:b" & R & ")"
End Sub
|
|