VBA 例程將資料夾中所有工作簿範圍內的所有值求和到另一個工作簿

VBA 例程將資料夾中所有工作簿範圍內的所有值求和到另一個工作簿

我所擁有的:我在某處找到了一個 VBA 例程,並試圖適應我的問題。據我了解,此例程會尋找資料夾中的所有 Excel 工作簿,並將所有檔案範圍 H8:H27 合併到一個新工作簿中。

我需要什麼:一個例程,用於查找資料夾中的所有 Excel 工作簿(不包括totals.xlsx),並將 Sheet(2)H8:H27 範圍內的值求和到工作簿totals.xlsx!板材(2)H8:H27

我有一個包含 67 個 Excel 工作簿的資料夾,其中包括一個名為 Totals.xlsx 的工作簿;

除了totals.xls 之外,其他工作簿的名稱都很大。所有書中的第2頁也有一個巨大的名字。

所有書籍的結構都相同;

我需要將範圍表(2)H8:H27 中的所有工作簿(不包括totals.xlsx)值求和到工作簿total.xls 中的相同範圍!板材(2)H8:H27;

我無法使用合併工具,因為限制為 50 個檔案;

幾乎不可能寫出一個公式來引用 67 個名稱巨大的工作簿,並且表(2)的名稱也很大;

所以我確實考慮過VBA例程將資料夾中所有工作簿(不包括totals.xlsx)的H8:H27範圍內的值求和到totals.xlsx工作簿的工作表(2)中的相同範圍

我找到並改編了以下 VBA 例程。我想我已經快到了,但到目前為止我已經能夠將這些值合併到一個單獨的工作簿中。不知道如何對所有工作簿求和(不包括totals.xlsx)!表 (2)H8:H27 至總計.xlsx!板材(2)H8:H27

Sub MergeAllWorkbooks()
    Dim MyPath As String, FilesInPath As String
    Dim MyFiles() As String
    Dim SourceRcount As Long, FNum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long
    MyPath = "C:\Users\test"
    If Right(MyPath, 1) <> "\" Then
        MyPath = MyPath & "\"
    End If
    FilesInPath = Dir(MyPath & "*.xl*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If
    FNum = 0
    Do While FilesInPath <> ""
        FNum = FNum + 1
        ReDim Preserve MyFiles(1 To FNum)
        MyFiles(FNum) = FilesInPath
        FilesInPath = Dir()
    Loop
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    rnum = 1

    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))
            On Error GoTo 0
            If Not mybook Is Nothing Then
                On Error Resume Next
                With mybook.Worksheets(1)
                    Set sourceRange = .Range("A1:C1")
                End With

                If Err.Number > 0 Then
                    Err.Clear
                    Set sourceRange = Nothing
                Else
                    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 "There are not enough rows in the target worksheet."
                        BaseWks.Columns.AutoFit
                        mybook.Close savechanges:=False
                        GoTo ExitTheSub
                    Else

                        With sourceRange
                            BaseWks.Cells(rnum, "A"). _
                                    Resize(.Rows.Count).Value = MyFiles(FNum)
                        End With
                        Set destrange = BaseWks.Range("B" & rnum)
                        With sourceRange
                            Set destrange = destrange. _
                                            Resize(.Rows.Count, .Columns.Count)
                        End With
                        destrange.Value = sourceRange.Value
                        rnum = rnum + SourceRcount
                    End If
                End If
                mybook.Close savechanges:=False
            End If
        Next FNum
        BaseWks.Columns.AutoFit
    End If
ExitTheSub:
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub

答案1

解決了!

子 SUM_WBs()

Dim FileNameXls As Variant, i As Integer, wb As Workbook

Range("H8:H27").ClearContents

FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*", MultiSelect:=True)
If Not IsArray(FileNameXls) Then Exit Sub

Application.ScreenUpdating = False

For i = LBound(FileNameXls) To UBound(FileNameXls)

    Set wb = Workbooks.Open(FileNameXls(i))
    wb.Sheets(2).Range("H8:H27").Copy
    ThisWorkbook.Sheets(2).Range("H8:H27").PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, SkipBlanks:=True, Transpose:=False
    Application.CutCopyMode = False
    wb.Close SaveChanges:=False

Next i

Application.ScreenUpdating = True

結束子

相關內容