Loop macro for excel & save

kshivanand21

New Member
Joined
Jan 19, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hey Guys...Need your expert help....How to code VBA code, all excel file in a folder need to be looped to run set of task on each excel file than saved in a specified folder & name containing from cell value.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
paste the code in to a module,
here the folder to use is in Cell A1, change if needed.
then run : ProcessAllFiles

add your code to ProcessMyFile() for what you want done to the open file.

Code:
Option Explicit
Public Sub ProcessAllFiles()
   'give the start folder
ScanFilesIn1Folder  range("A1").value    '"c:\temp\"
End Sub

Private Sub ScanFilesIn1Folder(ByVal pvStartDir)
Dim FileSystem As Object
Dim Folder As Object
Dim oFile As Object
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set Folder = FileSystem.GetFolder(pvStartDir)
For Each oFile In Folder.Files
    'If InStr(oFile.Name, "backup") > 0 Then GoTo skip1
   
    If InStr(oFile.Name, ".xls") > 0 Then  'only xl files
       ProcessMyFile oFile
    End If
   
skip1:
Next
Set oFile = Nothing
Set Folder = Nothing
Set FileSystem = Nothing
End Sub

Private Sub ProcessMyFile(ByVal poFile)
Workbooks.Open poFile
'do stuff here
ActiveWorkbook.Close True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,862
Messages
6,181,464
Members
453,044
Latest member
rgbenson1

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