VBA - extracting multiple worksheets from multiple workbooks to another workbook based on condition

Momo3005

New Member
Joined
Aug 20, 2015
Messages
1
Hi,

I'm trying to extract data from multiple worksheets from over 30 workbooks from a same path to a another master workbook. For instance, if cell D9 in the different worksheets is equal to "X", I want to extract data from specific cells for all those worksheets.

I am very new to VBA and I've tried this code and it doesnt work. Please help! Thanks a lot!

Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
Dim ws As Worksheet
Dim Filepath As String
Filepath = "D:\Users\xxx"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "Zmaster.xlsm" Then
Exit Sub
End If

Workbooks.Open (Filepath & MyFile)
For Each ws In Worksheet
If ws.Cells(9, 4) = "X" Then
ws.Range("I30:I32").Copy
ActiveWorkbook.Close

erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 4))
End If
Next ws
MyFile = Dir
Loop
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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