+ Reply to Thread
Results 1 to 3 of 3

Running macro in workbooks before copying ranges to Master Workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    10-16-2014
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2011 Mac
    Posts
    13

    Running macro in workbooks before copying ranges to Master Workbook

    I have been using this guide to merge a range from all workbooks in a folder: http://msdn.microsoft.com/en-us/libr...ffice.12).aspx
    (more specifically, the following code
    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
    
        ' Change this to the path\folder location of your files.
        MyPath = "C:\Users\Ron\test"
    
        ' Add a slash at the end of the path if needed.
        If Right(MyPath, 1) <> "\" Then
            MyPath = MyPath & "\"
        End If
    
        ' If there are no Excel files in the folder, exit.
        FilesInPath = Dir(MyPath & "*.xl*")
        If FilesInPath = "" Then
            MsgBox "No files found"
            Exit Sub
        End If
    
        ' Fill the myFiles array with the list of Excel files
        ' in the search folder.
        FNum = 0
        Do While FilesInPath <> ""
            FNum = FNum + 1
            ReDim Preserve MyFiles(1 To FNum)
            MyFiles(FNum) = FilesInPath
            FilesInPath = Dir()
        Loop
    
        ' Set various application properties.
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        ' Add a new workbook with one sheet.
        Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
        rnum = 1
    
        ' Loop through all files in the myFiles array.
        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
    
                    ' Change this range to fit your own needs.
                    With mybook.Worksheets(1)
                        Set sourceRange = .Range("A1:C1")
                    End With
    
                    If Err.Number > 0 Then
                        Err.Clear
                        Set sourceRange = Nothing
                    Else
                        ' If source range uses 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 "There are not enough rows in the target worksheet."
                            BaseWks.Columns.AutoFit
                            mybook.Close savechanges:=False
                            GoTo ExitTheSub
                        Else
    
                            ' Copy the file name in column A.
                            With sourceRange
                                BaseWks.Cells(rnum, "A"). _
                                        Resize(.Rows.Count).Value = MyFiles(FNum)
                            End With
    
                            ' Set the destination range.
                            Set destrange = BaseWks.Range("B" & rnum)
    
                            ' Copy the values from the source range
                            ' to the destination range.
                            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:
        ' Restore the application properties.
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
    End Sub
    I would like to add some VBA so that the macro "CopyRangeFromMultiWorksheets" runs in the workbook before the range is copied.

    I think it should be something like
    Application.Run "'" & mybook & "'!CopyRangeFromMultiWorksheets"
    but that isn't working.

    I would really appreciate any help!

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,293

    Re: Running macro in workbooks before copying ranges to Master Workbook

    Does the code exist in the workbook 'mybook' and you want to run it?

    Application.Run "'" & mybook.name & "'!CopyRangeFromMultiWorksheets"
    Or do you want to run that code while the workbook 'mybook' is active?

    CopyRangeFromMultiWorksheets
    which must be written in such a way as to act on either the activeworkbook or on a workbook object that is global.

    Or you could loop within this code:


    With mybook.Worksheets(1)
         Set sourceRange = .Range("A1:C1")
    End With

    becomes


    For Each Sht In mybook.Worksheets
         With Sht
              Set sourceRange = .Range("A1:C1")
         End With
    '... other code
    Next Sht
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    10-16-2014
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2011 Mac
    Posts
    13

    Re: Running macro in workbooks before copying ranges to Master Workbook

    I was looking for the first code - just missing the ".name" - thank you so much!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copying data from multiple workbooks to 1 master workbook
    By chilli76 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-15-2014, 12:50 PM
  2. Copying all EXCEL workbooks in a Folder to one master workbook
    By HitTheEXCELerator in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-10-2013, 10:42 PM
  3. [SOLVED] Copying data from other workbooks and pasting into master workbook
    By jcook1100 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-15-2013, 08:03 AM
  4. [SOLVED] Macro for Copying Cels from All Workbooks to a Single Master Workbook
    By JohnnyJ2013 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-25-2013, 05:51 AM
  5. Replies: 18
    Last Post: 05-11-2010, 09:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1