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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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