|
在EXCElHOME看了很多篇关于跨工作簿的帖子,也自己尝试了好多遍,但依旧无法整出来(太过于新手[捂脸])。这份表格最近一个礼拜需要整完,工作量大,自己编写的代码总是出现错误,希望大家可以帮帮忙!
- Sub test()
- Dim cnn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim sql As String
- Dim mybook$, mypath$, wjm$
- mypath = ThisWorkbook.Path & ""
- mybook = ThisWorkbook.FullName
-
- With cnn
- .Provider = "microsoft.jet.oledb.4.0"
- .ConnectionString = "extended properties=""excel 8.0;HDR=YES;"";data source=" & mybook
- .Open
- End With
-
- wjm = Dir(mypath & "*.xls")
- Do While wjm <> ""
- If wjm <> "工作簿汇总.xls" Then
- sql = sql & "union all select * from [Excel 8.0;Database=" & mypath & wjm & "].[工资表1$b4:l] where len(店铺名称)<>0 and 店铺名称<>'工作簿汇总' "
- End If
- wjm = Dir()
- Loop
- sql = Mid(sql, 4)
- rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
- With Worksheets("汇总表")
- .UsedRange.Offset(4, 0).Delete
- .Range("b5").CopyFromRecordset rs
- r = .Cells(.Rows.Count, 2).End(xlUp).Row
- .Range("a3:a" & r).FormulaR1C1 = "=row()-2"
- .Range("a" & r + 1) = "合计"
- .Range(.Cells(r + 1, 6), .Cells(r + 1, 19)).FormulaR1C1 = "=SUM(R[" & 2 - r & "]C:R[-1]C)"
- With .Range("a1:m" & r + 1)
- .Borders.LineStyle = xlContinuous
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- End With
- rs.Close
- cnn.Close
- End Sub
复制代码 |
|