Oct 06 2021 10:28 AM
I have been using this code since couple of months and I have number of workbooks on which i apply this code to save the exact copy of that workbook with all data.
I am doing this separately for each workbook. I want to apply this code by opening a new workbook, when i run the code it will give me option to select the folder. I want to apply this code on a folder which have multiple workbooks and also on their sub folder.
Your help will be much appreciated.
Sub stuff()
Dim DISTNAME As String
DISTNAME = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & "New Added" & ".xls"
DISTNAME = ActiveWorkbook.Path & Application.PathSeparator & DISTNAME
Sheets.Select
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
ActiveWorkbook.SaveAs DISTNAME
End Sub
Oct 06 2021 11:27 AM
Try this. Change the path in LoopFolder to the folder you want to process.
The code calls your Stuff macro.
Sub LoopFolder()
Const TopFolder = "C:\Excel\"
Dim fso As Object
Dim fld As Object
Dim fil As Object
Application.ScreenUpdating = False
Set fso = CreateObject(Class:="Scripting.FileSystemObject")
Set fld = fso.GetFolder(TopFolder)
Call ProcessFolder(fld)
Application.ScreenUpdating = True
End Sub
Sub ProcessFolder(ByVal fld As Object)
Dim sfl As Object
Dim fil As Object
For Each fil In fld.Files
If LCase(Right(fil.Name, 4)) = ".xls" Then
Workbooks.Open fil.Name
Call stuff
ActiveWorkbook.Close
End If
Next fil
For Each sfl In fld.SubFolders
Call ProcessFolder(sfl)
Next sfl
End Sub
Oct 06 2021 12:41 PM
Oct 06 2021 01:14 PM
SolutionIs this better?
Sub LoopFolder()
Dim TopFolder As String
Dim fso As Object
Dim fld As Object
Dim fil As Object
With Application.FileDialog(4) ' msoFileDialogFolderPicker
If .Show Then
TopFolder = .SelectedItems(1) & "\"
Else
Beep
Exit Sub
End If
End With
Application.ScreenUpdating = False
Set fso = CreateObject(Class:="Scripting.FileSystemObject")
Set fld = fso.GetFolder(TopFolder)
Call ProcessFolder(fld)
Application.ScreenUpdating = True
End Sub
Sub ProcessFolder(ByVal fld As Object)
Dim sfl As Object
Dim fil As Object
For Each fil In fld.Files
If LCase(Right(fil.Name, 4)) = ".xls" Then
Workbooks.Open fil.Name
Call stuff
ActiveWorkbook.Close
End If
Next fil
For Each sfl In fld.SubFolders
Call ProcessFolder(sfl)
Next sfl
End Sub
Oct 08 2021 04:56 AM
Oct 08 2021 08:13 AM
Does this work better?
Sub LoopFolder()
Const TopFolder = "C:\Excel\"
Dim fso As Object
Dim fld As Object
Dim fil As Object
Application.ScreenUpdating = False
Set fso = CreateObject(Class:="Scripting.FileSystemObject")
Set fld = fso.GetFolder(TopFolder)
Call ProcessFolder(fld)
Application.ScreenUpdating = True
End Sub
Sub ProcessFolder(ByVal fld As Object)
Dim sfl As Object
Dim fil As Object
Dim wbk As Workbook
For Each fil In fld.Files
If LCase(Right(fil.Name, 4)) = ".xls" Then
Set wbk = Workbooks.Open(Filename:=fil)
Call stuff(wbk)
wbk.Close SaveChanges:=False
End If
Next fil
For Each sfl In fld.SubFolders
Call ProcessFolder(sfl)
Next sfl
End Sub
Sub stuff(wbk As Workbook)
Dim DISTNAME As String
wbk.Activate
Sheets.Select
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
DISTNAME = Left(wbk.Name, Len(wbk.Name) - 4) & "New Added" & ".xls"
DISTNAME = wbk.Path & Application.PathSeparator & DISTNAME
wbk.SaveAs Filename:=DISTNAME, FileFormat:=xlExcel8
End Sub
Oct 08 2021 10:09 AM
Oct 08 2021 01:39 PM
Sorry, I cannot explain that.
Oct 11 2021 04:04 AM
Oct 06 2021 01:14 PM
SolutionIs this better?
Sub LoopFolder()
Dim TopFolder As String
Dim fso As Object
Dim fld As Object
Dim fil As Object
With Application.FileDialog(4) ' msoFileDialogFolderPicker
If .Show Then
TopFolder = .SelectedItems(1) & "\"
Else
Beep
Exit Sub
End If
End With
Application.ScreenUpdating = False
Set fso = CreateObject(Class:="Scripting.FileSystemObject")
Set fld = fso.GetFolder(TopFolder)
Call ProcessFolder(fld)
Application.ScreenUpdating = True
End Sub
Sub ProcessFolder(ByVal fld As Object)
Dim sfl As Object
Dim fil As Object
For Each fil In fld.Files
If LCase(Right(fil.Name, 4)) = ".xls" Then
Workbooks.Open fil.Name
Call stuff
ActiveWorkbook.Close
End If
Next fil
For Each sfl In fld.SubFolders
Call ProcessFolder(sfl)
Next sfl
End Sub