Multi-Wookbook Macro

JonReyno

Board Regular
Joined
Jun 8, 2009
Messages
130
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

Is it possible to have a Macro on a 'Master' Workbook which, when activated, adds the value of the same cell from 4 other Workbooks?

All of the 5 workbooks are exactly the same layout, the 1st one is the Master one which is where I would like the collate inforamtion from the other 4 to go to.

For example: Workbook 1, Cell A1 = 2... Workbook 2, Cell A1 = 1... Workbook 3, Cell A1 = 0 and Workbook 4, Cell A1 = 0. Matser Workbook then takes the information from Cell A1 of each of the workbooks and puts it in to Cell A1 of the Master Workbook to get a total, in this example 3...

I'm clutching at straws and don't know if it's possible, any help would be apprecaited.

Thanks
Jon:confused:
 
I have used a different approach..

Assumptions..
1. All 4 files are in a separate folder,
2. Master.xls is the name of WB where data will be collected and in sheet named "Summary"
3. Summary sheet have three columns A, B, C titles as Name, Month and Cost respectively.
4. Source files have 12 tabs for each month, and if there is any other tab then that should be the first one and change the value of i to 2..
5. You want to sum the values of G6:G7,G10:G12,G14:G18,G20:G27

What this macro does is it will prompt you to choose the folder where your files are then a loop will open each file regardless of number files, then it loop through each sheet of that opened and will copy values of G6:G7,G10:G12,G14:G18,G20:G27 and paste it to Master file Summary sheet, it takes the name from the file name (mid,12,99) and month from the sheet name(left,3).. and in the end it creates a PivotTable on the basis of data collected in Summary sheet..


<TABLE style="WIDTH: 200pt; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=265 border=0 x:str><COLGROUP><COL style="WIDTH: 79pt; mso-width-source: userset; mso-width-alt: 3840" width=105><COL style="WIDTH: 62pt; mso-width-source: userset; mso-width-alt: 2998" width=82><COL style="WIDTH: 59pt; mso-width-source: userset; mso-width-alt: 2852" width=78><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD class=xl22 style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; WIDTH: 79pt; BORDER-BOTTOM: #d4d0c8; HEIGHT: 12.75pt; BACKGROUND-COLOR: transparent" width=105 height=17>Name</TD><TD class=xl22 style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; WIDTH: 62pt; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" width=82>Month</TD><TD class=xl22 style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; WIDTH: 59pt; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent" width=78>Cost</TD></TR></TBODY></TABLE>


Code:
Sub MainMacro()
    Dim LR, FR As Long
    Dim n, m As String
    Dim xDirect$, xFname$, InitialFoldr$
    Dim WS_Count As Integer
    Dim i As Integer
 
    'Change ScreenUpdating, Calculation and EnableEvents
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
 
    InitialFoldr$ = "S:\Power Networks\Asset Management\SND\OPC\WAYLEAVES\Balance Score Card\"    '<<< Startup folder to begin searching from
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select the folder where files are"
        .InitialFileName = InitialFoldr$
        .Show
        If .SelectedItems.Count <> 0 Then
            xDirect$ = .SelectedItems(1) & "\"
            xFname$ = Dir(xDirect$, 7)
            Do While xFname$ <> ""
 
            Workbooks.Open (xDirect$ & xFname$), UpdateLinks:=False
            n = Replace(Mid(xFname$, 12, 99), ".xls", "")
            WS_Count = ActiveWorkbook.Worksheets.Count
   For i = 1 To WS_Count
 
            Windows(xFname$).Activate
            Sheets(i).Select
            m = Left(ActiveWorkbook.Worksheets(i).Name, 3)
            Windows("Master.xls").Activate
            Sheets("Summary").Select
            LR = Range("A" & Rows.Count).End(xlUp).Row + 1
            Range("B" & LR).Select
            ActiveCell.Value = m
            Range("A" & LR).Select
            ActiveCell.Value = n
            Windows(xFname$).Activate
            Range("G6:G7,G10:G12,G14:G18,G20:G27").Select
            Selection.Copy
            Windows("Master.xls").Activate
            Sheets("Summary").Select
            Range("C" & LR).Select
            ActiveSheet.Paste
            FR = Range("A" & Rows.Count).End(xlUp).Row
            LR = Range("C" & Rows.Count).End(xlUp).Row
            Range("A" & FR & ":B" & LR).Select
            Selection.FillDown
   Next i
    Windows(xFname$).Activate
    ActiveWindow.Close savechanges:=False
    Range("A1").Select
 
                xFname$ = Dir
 
            Loop
         End If
    End With
 
    'Restore ScreenUpdating, Calculation and EnableEvents
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
        .Calculation = xlCalculationAutomatic
    End With
 
    'PT
    Windows("Master.xls").Activate
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        Sheets("Summary").Range("A1").CurrentRegion.Address).CreatePivotTable TableDestination:="", TableName:= _
        "PivotTable1", DefaultVersion:=xlPivotTableVersion10
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select
    ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="Name", _
        ColumnFields:="Month"
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Cost").Orientation = _
        xlDataField
    Range("A1").Select
End Sub

hope this meets your requirement..
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Ravi,

Thanks for taking the time to look into this for me. I have been dealing with someone else on this and we are in the process of fine tuning the code.

Should the code we are working on fail, then I shall look into the code you have kindly provided to see if that works.

Many Thanks
Jon
 
Upvote 0
Hi JC,

The data that I am trying to collate is information from 4 of my managers. Each month they need to put a figure in a spreadsheet and forward it on to me. I then will get the data from all 4 spreadsheets to add them together to get a total. The vast majority of the figures are just numbers, but there are about 3 cells which are a percentage and a couple which are a financial value.

I'm not sure where to go from here then as I'm getting a bit confused with the DIM situation (ironic it's DIM and that is how I'm feeling about all this).

Any ideas what I should do?
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top