Question on accessing multiple workbooks to form a master database

NPike

New Member
Joined
Jan 22, 2009
Messages
17
Please Help!

I have 100 or so workbooks named A1234.xls, A1235.xls, A1236.xls etc.

I want to gather information from the same worksheet and same cell in each workbook.

I want this information in a separate workbook named Master.xls

In the Master spreadsheet I have the workbook names in column A e.g.
A1234
A1235
A1236 etc.

In column B, I want the information form each of the 100 workbooks. For example, from "sheet 1" Cell C2. This is the same place I want the information from in each workbook.

Is there an easy way of doing this with a formulae rather than a macro. For example, in the Master spreadsheet column B1 formula would read =[A1234.xls]Sheet1!$C$2 ... and then can you drag this changing the filename according to column A in the master spreadsheet?

If not can a macro be used? I have only started looking at macro's and my knowledge on them is very basic.

Any help would be very much appreciated.

Thanks in advance!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
NPike,

Here you go.

Tested with four workbooks, and in each cell C2, is the four digit number of the file name (as an example).


Before the macro:

Excel Workbook
AB
1Workbook NameC2 Value
2A1234
3A1235
4A1236
5A1237
6A1238
Sheet1



After the macro:

Excel Workbook
AB
1Workbook NameC2 Value
2A12341234
3A12351235
4A12361236
5A12371237
6A12381238
Sheet1



Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Press and hold down the 'ALT' key, and press the 'F11' key.

On the 'Insert' menu, click 'Module'.

Copy the below code, and paste it into the Module (on the right pane).


Code:
Option Explicit
Sub GetData()
    Dim MyDir, FileName, SheetName As String
    Dim LR, Ctr As Long
    Application.ScreenUpdating = False
    
    '**************************************************
    'Change MyDir to your directory/folder path
    'MyDir = "C:\TestData"
    '**************************************************
    
    MyDir = "C:\TestData"


    '**************************************************
    'Change SheetName to your sheetname
    'SheetName = "Sheet1"
    '**************************************************
    
    SheetName = "Sheet1"


    With ActiveSheet
        LR = .Cells(Rows.Count, 1).End(xlUp).Row
        For Ctr = 2 To LR Step 1
            FileName = Cells(Ctr, 1).Value & ".xls"
            With .Cells(Ctr, 2)
                .Formula = "='" & MyDir & "\[" & FileName & "]" & SheetName & "'!C2"
                .Value = .Value
            End With
        Next Ctr
    End With
    Application.ScreenUpdating = True
End Sub


Make sure you change the "MyDir = "C:\TestData" in the above code to match your directory/folder path.


Then run the "GetData" macro.


Have a great day,
Stan
 
Upvote 0
Thanks Stan!

Works really well. More complicated than I first thought!

If I was wanting to change the cell I was copying from each workbook from C2 to say another cell, for example AL203, what part of the Macro do I have to alter and how?

Also if I was wanting to change the destination in the Master workbook from B2 to say D8, what part of the Macro do I have to alter?

Thanks once again.
 
Upvote 0
NPike,

The answer to your questions are in the below code.

Code:
Option Explicit
Sub GetData()
    Dim MyDir, FileName, SheetName As String
    Dim LR, Ctr As Long
    Application.ScreenUpdating = False
    
    '**************************************************
    'Change MyDir to your directory/folder path
    'MyDir = "C:\TestData"
    '**************************************************
    
    MyDir = "C:\TestData"


    '**************************************************
    'Change SheetName to your sheetname
    'SheetName = "Sheet1"
    '**************************************************
    
    SheetName = "Sheet1"


    With ActiveSheet
        LR = .Cells(Rows.Count, 1).End(xlUp).Row
        
        'If your File Names in column A begin in row 8
        'change the next line of code
        'For Ctr = 2 To LR Step 1
        'to
        For Ctr = 8 To LR Step 1
            FileName = Cells(Ctr, 1).Value & ".xls"
            
            'If you want to change the destination cell from B2 To D8
            'change the next line of code
            'With .Cells(Ctr, 2)
            'to
            With .Cells(Ctr, 4)
            
                'copying from each workbook from C2 to say another cell, for example AL203
                'change the next line of code
                '.Formula = "='" & MyDir & "\[" & FileName & "]" & SheetName & "'!C2"
                'to
                .Formula = "='" & MyDir & "\[" & FileName & "]" & SheetName & "'!AL203"
                
                .Value = .Value
            End With
        Next Ctr
    End With
    Application.ScreenUpdating = True
End Sub


Have a great day,
Stan
 
Upvote 0
Stan,

Thanks for the help. I have tried to alter the macro slightly as aforementioned. The problem I am getting now is everytime I run the Macro an "Update Values" box pops up and I have to choose the desitnation folder and file. This did not happen in your original example and if possible I would not like it to happen on my altered spreadsheet. Can you spread any light on this for me? I have copied the code I am using below.

For reference:

In the spreadsheet I am running the Macro, the scheme names start in E11, and the results, I want to start in F11. The spreadsheets I am getting the information from are all cell D31. Hope this gives you enough detail.

Thank you very much.

Neil.



Option Explicit
Sub GetData()
Dim MyDir, FileName, SheetName As String
Dim LR, Ctr As Long
Application.ScreenUpdating = False

'**************************************************
'Change MyDir to your directory/folder path
'MyDir = "C:\TestData"
'**************************************************

MyDir = "K:\Lee\CM\YWS\Area Framework Cost Validation\Test Schemes"

'**************************************************
'Change SheetName to your sheetname
'SheetName = "Sheet1"
'**************************************************

SheetName = "Scheme Overview"

With ActiveSheet
LR = .Cells(Rows.Count, 5).End(xlUp).Row
For Ctr = 11 To LR Step 1
FileName = Cells(Ctr, 1).Value & ".xls"
With .Cells(Ctr, 6)
.Formula = "='" & MyDir & "\[" & FileName & "]" & SheetName & "'!D31"
.Value = .Value
End With
Next Ctr
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Stan I was wondering if you could just give me one last help? With the code you gave me before, I have been able to adjust it to suit accordingly. There is just one last thing which I am struggling with.

In the worksheet with the Macro in, column A has the workbook scheme names in, and column B has the returned values I am looking for in each workbook.

How do I change the Macro so that the scheme names (e.g. A1234) are in say column F?

Thanks once again for your help.

Neil.
 
Upvote 0
If you don't want to use a macro, why not try Data > Consolidate? I use this to consolidate one row of data from each workbook, up to around 100 or more in some case. It's easy to set up - in the source workbooks, create a named range including header row (the header row should be the same in each file), then just use the above function to consolidate the same range name for each different file.

Regards,
Stuart
 
Upvote 0
Stuart,

I would much prefer not to use a Macro. Can you expand on this method you described. Note. The main problem I have is there is around 2000 workbooks and I can't drag the formulae in the Summary spreadsheet to change file references.

Cheers, Neil.
 
Upvote 0
NPike,

Stan I was wondering if you could just give me one last help? With the code you gave me before, I have been able to adjust it to suit accordingly. There is just one last thing which I am struggling with.

In the worksheet with the Macro in, column A has the workbook scheme names in, and column B has the returned values I am looking for in each workbook.

How do I change the Macro so that the scheme names (e.g. A1234) are in say column F?


Screenshots would really help.

Please post a screenshot of your sheet(s), what you have and what you expect to achieve, with Excel Jeanie HTML 4.
http://www.excel-jeanie-html.de/index.php?f=1

Instructions for using "Excel Jeanie HTML 4":
http://www.excel-jeanie-html.de/html/hlp_schnell_en.php


Have a great day,
Stan
 
Upvote 0
Hi Stan,

Hope this helps.

I actually have a few more functions with the macro's I need to use.

1) The first one is to copy the cells below (which are on all the the 1000's of spreadsheets named A1234 etc) - AL23 to AQ23

Excel Workbook
AJAKALAMANAOAPAQAR
17
18
19
20
21
22Gateway 1Gateway 2Gateway 3Gateway 4Gateway 5Gateway 6
2319-Jan-0919-Jan-0919-Jan-0919-Jan-0919-Jan-0919-Jan-09
24
25
Scheme Overview


Copy onto the spreadsheet below (the Master Summary workbook) - B11 to G485

Excel Workbook
ABCDEFGH
3GATEWAY PROCESS MAP
4
5%
622%0%11%0%0%0%Nr.
7Scheme Nr.201000Scheme
89
9
10SchemesOriginally ReceivedReceived Information all CompleteCommencement of Level ChecksIssues IdentifiedIssues ResolvedRecommendations made
11A123428-Jan-0901-Jan-091
12A123530-Jan-092
13AMP4-in-4 A1236 61330-Jan-093
14etc4
Gateways


2) Also need to copy these cells from the 1000's of workbooks, from AL37 to AS37

Excel Workbook
AKALAMANAOAPAQARASAT
34
35General CostsAll "People" CostsEquipment CostsMaterial CostsAOH CostsAsset LivesOpexGeneral
36
3700010000000001E+06
38
39
Scheme Overview


To the Master summary workbook - I486 to P486.

Excel Workbook
HIJKLMNOP
483A12321000000100000010000001000000
484A12331000000
485A123400010000000001000000
486
487SchemeGeneral CostsAll "People" CostsEquipment CostsMaterial CostsAOH CostsAsset LivesOpexGeneral
Data



3) And lastly, copy from the 1000's of workbooks AJ102 to IU102
Excel Workbook
AIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBE
97
98
99
100R
101L1Q1L1Q2L1Q3L1Q4L1Q5L1Q6L1Q7L1Q8L1Q9L1Q10L1Q11L1Q12L1Q13L1Q14L1Q15L1Q16L1Q17L1Q18L1Q19L1Q20L1Q21L1Q22
1020100000001000000000000
Scheme Overview


Copied to, E10 to HP10 in the Master Summary workbook.

Excel Workbook
DEFGHIJKLMNO
6
7Reds
8Totals01000000010
9SchemeL1Q1L1Q2L1Q3L1Q4L1Q5L1Q6L1Q7L1Q8L1Q9L1Q10L1Q11
10A123401000000010
11A12350
12AMP4-in-4 A1236 6130
13etc0
1400
1500
Areas of Concern



I am so greatful for your help, you are saving my life!

Cheers Stan,

Neil.
 
Upvote 0

Forum statistics

Threads
1,215,234
Messages
6,123,773
Members
449,123
Latest member
StorageQueen24

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