Getting data from multiple workbooks (+200) using Index & Match in VBA (?)

M2909

New Member
Joined
Jan 30, 2018
Messages
10
Hi all,

This is my first post on this forum, you guys already helped me a lot by answering other people's questions.
But now i can't really find a solution.

We have on a yearly basis, a total of 208 workbooks (1 for each hal, p. week)
now i want to extract a certain field of data to a masterfile.

I have written below Index&Match foruma and this works, but it is very time consuming becaus each formula has to be adjusted to the exact path (manually)

=INDEX(PathToFile.xlsx]*sheetname*'!$B$7:$N$36;MATCH(BP$1;'PathToFile.xlsx]*sheetname*'!$A$7:$A$36;0);MATCH($A390;'PathToFile.xlsx]*Sheetname*'!$B$5:$N$5;0))

So now i'm trying my luck in VBA, it's all pretty new to me (bought a book this weekend) and by digging into the macros which i find on the net i'm learning every day.
Below Macro i've written based on information i found on the net:

it kind of works, but it just pastes the Range into my masterfile in a following order, without checking wether i'ts in the right row (which refers to the exact date)

Hope you can help! Thanks!

Public Sub test()
'DECLARE AND SET VARIABLES
Dim wbk As Workbook
Dim Filename As String
Dim Path As String


Application.ScreenUpdating = False


Path = "W:\Path"
Filename = Dir(Path & "*.xlsx")
'--------------------------------------------
'OPEN EXCEL FILES
Do While Len(Filename) > 0 'IF NEXT FILE EXISTS THEN
Set wbk = Workbooks.Open(Path & Filename)
Sheets("Weekoverzicht").Select
ActiveSheet.Unprotect Password:="******"
Range("B7:z29").SpecialCells(xlCellTypeVisible).Copy
Sheet1.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveSheet.protect Password:="******"


'Prevents question for copy clipboard
Application.CutCopyMode = False


wbk.Close True
Filename = Dir
Loop
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi. It looks like you are pasting the data back into the workbook where the data came from not into your summary workbook. I hope you have kept copies of these 200 workbooks because you then save them at the end. Which workbook/ sheet do you need the data pasted to?
 
Upvote 0
Hi. It looks like you are pasting the data back into the workbook where the data came from not into your summary workbook. I hope you have kept copies of these 200 workbooks because you then save them at the end. Which workbook/ sheet do you need the data pasted to?

Hi Steve,

im pasting it in:

Sheet1.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True

since that workbook i open when the macro runs.
 
Upvote 0
Hi. It looks like you are pasting the data back into the workbook where the data came from not into your summary workbook. I hope you have kept copies of these 200 workbooks because you then save them at the end. Which workbook/ sheet do you need the data pasted to?


There is actually no need to save them, which aspect is the VBA is saving the file now?
 
Upvote 0
Which workbook are you wanting to paste in? Your code will paste into the workbook that has been opened provided it has a sheet with the code name Sheet1. The 'True' on wbk.Close True saves the file. Make it False.
 
Upvote 0
Ok, changed it to False, thanks!


it needs to be pasted in:
W:\Quality\2018\DC.xlsx
in WorkSheet: Totals

In this sheet column A = date (1-1-2018 till 31-12-2018)
Row 1 is Machinenumber (1 p. column)
 
Upvote 0
Ok so where have you placed the vba code? Which workbook is that in? Im not sure what you mean by the date and machine number??
 
Upvote 0
Ok, the workbook is W:\Quality\2018\DC.xslm
the vba code is in this workbook to.

the data has to be pasted in WorkSheet: Totals

Where it has be to pasted:
[TABLE="width: 439"]
<colgroup><col><col><col><col><col span="5"></colgroup><tbody>[TR]
[TD]Date[/TD]
[TD]Year[/TD]
[TD]Month [/TD]
[TD]Week[/TD]
[TD]A01[/TD]
[TD]A02[/TD]
[TD]A03[/TD]
[TD]A04[/TD]
[TD]A05[/TD]
[/TR]
[TR]
[TD="align: right"]1-10-2017[/TD]
[TD="align: right"]2017[/TD]
[TD]okt[/TD]
[TD="align: right"]40[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2-10-2017[/TD]
[TD="align: right"]2017[/TD]
[TD]okt[/TD]
[TD="align: right"]40[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]3-10-2017[/TD]
[TD="align: right"]2017[/TD]
[TD]okt[/TD]
[TD="align: right"]40[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]4-10-2017[/TD]
[TD="align: right"]2017[/TD]
[TD]okt[/TD]
[TD="align: right"]40[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]5-10-2017[/TD]
[TD="align: right"]2017[/TD]
[TD]okt[/TD]
[TD="align: right"]40[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

What has to be copied:
[TABLE="width: 856"]
<colgroup><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD][/TD]
[TD]1-1-2018[/TD]
[TD]2-1-2018[/TD]
[TD]3-1-2018[/TD]
[TD]4-1-2018[/TD]
[TD]5-1-2018[/TD]
[/TR]
[TR]
[TD]Machine[/TD]
[TD]Totaal %[/TD]
[TD]Totaal %[/TD]
[TD]Totaal %[/TD]
[TD]Totaal %[/TD]
[TD]Totaal %[/TD]
[/TR]
[TR]
[TD]A01[/TD]
[TD]108%[/TD]
[TD]108%[/TD]
[TD]91%[/TD]
[TD]107%[/TD]
[TD]108%[/TD]
[/TR]
[TR]
[TD]A02[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]A03[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]A04[/TD]
[TD]0%[/TD]
[TD]3%[/TD]
[TD]84%[/TD]
[TD]102%[/TD]
[TD]104%[/TD]
[/TR]
[TR]
[TD]A05[/TD]
[TD]5%[/TD]
[TD]50%[/TD]
[TD]123%[/TD]
[TD]119%[/TD]
[TD]118%
[/TD]
[/TR]
</tbody>[/TABLE]

Hope this makes it clear?
i can't attach the file..
 
Upvote 0
I paste it using Transpose because i want the date vertically in column A, where the source is vice versa.
 
Upvote 0
See if this does what you want:

Code:
Public Sub test()
'DECLARE AND SET VARIABLES
Dim wbk As Workbook
Dim Filename As String
Dim Path As String

Application.ScreenUpdating = False

Path = "W:\Path\"
Filename = Dir(Path & "*.xlsx")
'--------------------------------------------
'OPEN EXCEL FILES
Do While Len(Filename) > 0 'IF NEXT FILE EXISTS THEN
    Set wbk = Workbooks.Open(Path & Filename)
    With Sheets("Weekoverzicht")
        .Unprotect Password:="******"
        .Range("B7:Z29").SpecialCells(xlCellTypeVisible).Copy
        ThisWorkbook.Sheets("Totals").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        .Protect Password:="******"
    End With
    'Prevents question for copy clipboard
    Application.CutCopyMode = False
    wbk.Close False
    Filename = Dir
Loop

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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