JaimeMabini
New Member
- Joined
- Dec 29, 2021
- Messages
- 14
- Office Version
- 365
- Platform
- Windows
Hello,
The code below will display the folder picker dialog box to allow the user to specify the folder (directory) that they want to use. It will then loop through all the files within that folder. It will open the workbook, perform an action and then close it saving the changes made.
(1.) The job performs accordingly to the first workbook in the folder but doesn't loop to the next workbook.
(2.) Also, It doesn't automatically close and save the workbook after the job ends.
I believe this line should do the job but somehow the macro ends before it reaches this lines. it performs the job on the first workbook on the folder but don't close and save and loop through the next workbook.
Any help will be greatly appreciated.
Thank you in advance.
The code below will display the folder picker dialog box to allow the user to specify the folder (directory) that they want to use. It will then loop through all the files within that folder. It will open the workbook, perform an action and then close it saving the changes made.
(1.) The job performs accordingly to the first workbook in the folder but doesn't loop to the next workbook.
(2.) Also, It doesn't automatically close and save the workbook after the job ends.
VBA Code:
Sub AllWorkbooksSecond()
Dim MyFolder As String 'Path collected from the folder picker dialog
Dim MyFile As String 'Filename obtained by DIR function
Dim wbk As Workbook 'Used to loop through each workbook
Dim i As Long
Dim f As Range, c As Range
Dim message
Sheets("RDS Converter").Select
Z = Range("I7").Value 'Row range collected from the RDS Converter sheet
y = Range("I6").Value 'Path collected from the RDS Converter sheet
PathName = Range("I5").Value 'Path collected from the RDS Converter sheet
'On Error Resume Next
Application.ScreenUpdating = False
Application.EnableEvents = False
'Opens the folder picker dialog to allow user selection
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
MyFile = Dir(MyFolder) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore
Do While MyFile <> ""
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)
'below is the statements macro to perform
With Workbooks.Open(Filename:=MyFolder & MyFile).Sheets("OKTOP® CONFIGURATOR")
For Each c In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
If c.Row > Z Then
MsgBox ("Row " & Z & " Reached")
End
Else
Set f = Workbooks.Open(PathName & y).Sheets("OKTOP® CONFIGURATOR").Range("A:A").Find(c.Value, , xlValues, xlWhole, , , False)
If Not f Is Nothing Then
f.EntireRow.Copy
.Range("A" & c.Row).PasteSpecial xlValues
.Range("T" & c.Row).Value = "Yes"
Else
.Range("T" & c.Row).Value = "No"
End If
End If
Next
End With
wbk.Close Savechanges:=True 'close and save the workbook
f.Close Savechanges:=True 'close and save the workbook
MyFile = Dir 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = True
Application.CutCopyMode = False
End Sub
I believe this line should do the job but somehow the macro ends before it reaches this lines. it performs the job on the first workbook on the folder but don't close and save and loop through the next workbook.
VBA Code:
wbk.Close Savechanges:=True 'close and save the workbook
f.Close Savechanges:=True 'close and save the workbook
MyFile = Dir 'DIR gets the next file in the folder
Loop
Any help will be greatly appreciated.
Thank you in advance.