我有一個代碼,它將來自多個工作簿(但僅限於一張工作表)的數據合併到摘要工作簿中。我正在努力改變它的代碼與多個工作表的多個工作簿,但不能這樣做。您可以請幫忙:將來自多個工作簿的數據與多個工作表合併爲摘要工作簿
Sub MergeAllWorkbooks()
Dim myPath As String, FilesInPath As String, lastrow As String
Dim MyFiles() As String
Dim SourceRcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet, mysht As Worksheet
Dim sourceRange As Range, destRange As Range
Dim rnum As Long, CalcMode As Long
Dim i As Integer, j As Integer
'Fill in the path\folder where the files are
myPath = ThisWorkbook.Path & "\Some"
'Add a slash at the end if the user forget it
If Right(myPath, 1) <> "\" Then
myPath = myPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = dir(myPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
Set BaseWks = ThisWorkbook.Worksheets(3)
rnum = 1
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(myPath & MyFiles(Fnum))
Set mysht = mybook.Worksheet
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
'For i = 1 To Worksheets(i).Count
'LastRow = Worksheets(i).Range("F" & rows.Count).End(xlUp).Row
'MsgBox LastRow
With mybook.Worksheets(1)
Set sourceRange = Range("A6:I100") ' & LastRow)
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.rows.Count
If rnum + SourceRcount >= BaseWks.rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close SaveChanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
'For j = 1 To Worksheets(j).Count 'Worksheets.Count
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.rows.Count).Value = Range("A2").Value 'MyFiles(Fnum)
End With
'Next j
'Set the destrange
Set destRange = BaseWks.Range("B" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destRange = destRange. _
Resize(.rows.Count, .Columns.Count)
End With
destRange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
'Next i
mybook.Close SaveChanges:=False
End If
Next Fnum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
由於您的問題有*我正在努力與代碼*和*您可以請幫助*目前還不清楚,如果您當前的代碼錯誤,當運行或如果它是缺少你想要的部分。基於此,人們不太可能會嘗試使用您的代碼並嘗試閱讀您的想法。爲了得到一個答案(我假設你錯過了你想要的部分),我建議你加入一個嘗試,並添加有關哪個部分沒有按需要做的信息。 –