|
这段代码的作用是同一文件夹下子文件夹的工作簿汇总在汇总簿那个表中,我问了一下原创者,希望代码不受sheet名字影响,要用codename来引用,但是我琢磨了一下原代码文件,我将红色那句改了,老是显示下标越界等问题,或者黄色那句话出错,请问要怎么修改使得可以将同在一文件夹下的子文件夹不受sheet.name影响,汇总,谢谢
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With CreateObject("Wscript.Shell")
ListFileArr = Split(.exec("cmd /c dir /a-d /b /s " & Chr(34) & ThisWorkbook.Path & Chr(34)).StdOut.ReadAll, vbCrLf)
ListFileArr = Filter(ListFileArr, ".xl")
End With
Set wb0 = ThisWorkbook
DataRows = Sheet1.Cells(Sheet1.Cells.Rows.Count, 2).End(xlUp).Row
If DataRows >= 2 Then Sheet1.Rows("2:" & DataRows).ClearContents
For i = 0 To UBound(ListFileArr)
DataRows = Sheet1.Cells(Sheet1.Cells.Rows.Count, 2).End(xlUp).Row + 1
mypath = ListFileArr(i)
TmpName = Split(mypath, "\")(UBound(Split(mypath, "\")))
If mypath <> ThisWorkbook.FullName Then
Set wb = Workbooks.Open(mypath)
For Each sh In wb.Worksheets
If sh.Name = "Sheet0" Then
LastRow = sh.Cells(sh.Cells.Rows.Count, 2).End(xlUp).Row
sh.Range("B2:F" & LastRow).Copy
With wb0.Sheets(Sheet0.Name)
.Cells(2, DataRows).PasteSpecial Paste:=xlPasteValues
DataRowsE = .Cells(.Cells.Rows.Count, 2).End(xlUp).Row
.Range("G" & DataRows & ":" & "G" & DataRowsE) = TmpName
End With
Exit For
End If
Next sh
wb.Close
End If
Next i
MsgBox "数据汇总完毕!"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
我
|
|