excelboy92
New Member
- Joined
- Dec 17, 2021
- Messages
- 3
- Office Version
-
- 365
- 2021
- 2016
- Platform
-
- Windows
I've been trying to build a spreadsheet to import data from specific cells in workbooks to a master workbook. So far, I've been able to get this VBA working when it's just a set of workbooks within a single folder. However, we typically store these workbooks in a series of subfolders formatted C:\Users\user\Documents\Data\Month\Date\OrderType . All the workbooks are in the final OrderType folders. Can anyone help me adapt this macro to import data from subfolders?
This is the code I'm using to import data from one folder during my tests so far:
I attempted to use multiple pieces of VBA I found online in combination with this to pull the data from subfolders, but was ultimately unable to get it to work. Thanks in advance for any advice/code, I appreciate it!
This is the code I'm using to import data from one folder during my tests so far:
VBA Code:
Public Sub Copy_Values_From_Workbooks()
Dim matchWorkbooks As String
Dim destSheet As Worksheet, r As Long
Dim folderPath As String
Dim wbFileName As String
Dim fromWorkbook As Workbook
'Folder path and wildcard workbook files to import cells from
matchWorkbooks = "C:\Users\users\Desktop\Test Workbooks\*.xlsm" 'CHANGE THIS
'Define destination sheet
Set destSheet = ActiveWorkbook.Worksheets("Sheet1") 'CHANGE THIS
r = 0
Application.ScreenUpdating = False
folderPath = Left(matchWorkbooks, InStrRev(matchWorkbooks, "\"))
wbFileName = Dir(matchWorkbooks)
While wbFileName <> vbNullString
Set fromWorkbook = Workbooks.Open(folderPath & wbFileName)
With fromWorkbook.Worksheets("Cover Sheet")
destSheet.Range("E3").Offset(r).Value = .Range("E24").Value
destSheet.Range("F3").Offset(r).Value = .Range("E25").Value
destSheet.Range("C3").Offset(r).Value = .Range("C6").Value
destSheet.Range("G3").Offset(r).Value = .Range("N24").Value
destSheet.Range("H3").Offset(r).Value = .Range("N25").Value
destSheet.Range("D3").Offset(r).Value = .Range("C10").Value
End With
With fromWorkbook.Worksheets("Order Data")
destSheet.Range("I3").Offset(r).Value = .Range("G29").Value
destSheet.Range("J3").Offset(r).Value = .Range("G31").Value
r = r + 1
End With
fromWorkbook.Close savechanges:=False
DoEvents
wbFileName = Dir
Wend
Application.ScreenUpdating = True
MsgBox "Finished"
End Sub
I attempted to use multiple pieces of VBA I found online in combination with this to pull the data from subfolders, but was ultimately unable to get it to work. Thanks in advance for any advice/code, I appreciate it!