singlespeedmtb
New Member
- Joined
- Mar 28, 2016
- Messages
- 3
I have a set of code written to update some cells in a number of excel files that all share the same general format. I am able to get this code to work on all files within a folder but we literally have a directory of hundreds of subfolders organizing our files. I need to be able to loop through all of the files within these subfolders to fun these updates. Can anyone take a look and see what I am missing here? I've spend the last 3 hours on google trying to find the solution and it looks like I need to be using the fso function, but I have no idea how to incorporate that into my existing code. Here is the code I have:
Sub Update_All_Workbooks_In_Folder()
Dim MyFolder As String
Dim MyFile As String
Dim wbk As Workbook
'On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'Exit if none selected
MsgBox "You did not select a folder”"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assigns selected folder to MyFolder
End With
MyFile = Dir(MyFolder) 'DIR retrieves the first file in MyFolder
'Loop through all files in a folder
Do While MyFile <> “”
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)
Application.AskToUpdateLinks = False
Sheets("Blank Template").Range("E2").Formula = "=vlookup(warehouse,Warehouse!A:B,2,0)"
Sheets("Blank Template").Range("E2").VerticalAlignment = xlTop
Sheets("Blank Template").Range("E3").Value = "=vlookup(warehouse,Warehouse!A:E,5,0)"
Sheets("Blank Template").Range("E4").Value = "=(VLOOKUP(warehouse,Warehouse!A:G,6,0)&VLOOKUP(warehouse,Warehouse!A:H,7,0)&VLOOKUP(warehouse,Warehouse!A:H,8,0))"
Sheets("Blank Template").Range("F5").Value = "=VLOOKUP(warehouse,Warehouse!A:D,4,0)"
wbk.Close savechanges:=True
MyFile = Dir 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = True
End Sub
Sub Update_All_Workbooks_In_Folder()
Dim MyFolder As String
Dim MyFile As String
Dim wbk As Workbook
'On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'Exit if none selected
MsgBox "You did not select a folder”"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assigns selected folder to MyFolder
End With
MyFile = Dir(MyFolder) 'DIR retrieves the first file in MyFolder
'Loop through all files in a folder
Do While MyFile <> “”
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)
Application.AskToUpdateLinks = False
Sheets("Blank Template").Range("E2").Formula = "=vlookup(warehouse,Warehouse!A:B,2,0)"
Sheets("Blank Template").Range("E2").VerticalAlignment = xlTop
Sheets("Blank Template").Range("E3").Value = "=vlookup(warehouse,Warehouse!A:E,5,0)"
Sheets("Blank Template").Range("E4").Value = "=(VLOOKUP(warehouse,Warehouse!A:G,6,0)&VLOOKUP(warehouse,Warehouse!A:H,7,0)&VLOOKUP(warehouse,Warehouse!A:H,8,0))"
Sheets("Blank Template").Range("F5").Value = "=VLOOKUP(warehouse,Warehouse!A:D,4,0)"
wbk.Close savechanges:=True
MyFile = Dir 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = True
End Sub