用VBA汇总同文件下多工作簿某一工作表到汇总工作簿的某一工作表
3个回答
展开全部
刚给人写过一个,你参考一下!
Option Explicit
Sub main()
On Error Resume Next
Dim wk As Workbook, sht As Worksheet
Dim r As Variant, i As Integer
Dim pthh As String
pthh = "C:\Users\test\Desktop\test\"
Set sht = ThisWorkbook.Sheets("Sheet1")
r = ffiles(pthh & "*.xls")
For i = LBound(r) To UBound(r)
Set wk = Application.Workbooks.Open(pthh & r(i))
wk.Visible = False
sht.Range("e" & i + 1) = wk.Sheets("Sheet1").[b5]
wk.Close False
Set wk = Nothing
Next i
Set sht = Nothing
ThisWorkbook.Save
MsgBox "遍历复制完毕"
End Sub
Private Function ffiles(ByVal pth As String) As Variant
Dim isof As Variant
Dim arr() As Variant, k As Integer
k = 1
ReDim arr(1 To k)
isof = Dir(pth)
arr(k) = isof
Do While isof <> ""
k = k + 1
isof = Dir
ReDim Preserve arr(1 To k)
arr(k) = isof
Loop
ffiles = arr
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |