VBA opening exels with different names same location

GerrardSVK

New Member
Joined
Sep 18, 2023
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
Hello all I need an VBA code that will works like this.

1: I have folder where there is around 90 exel files with different names each is specifc.
2: Vba Has to open each of them one by one and execute macro that I have allready made.

So I need open first run macro close it than open second run macro close it etc. But it have to be unsensitive to xlsm name.

Is there any option?
 
Try such code in separate file. Of course use your macro instead of test and your folder path:

VBA Code:
Sub run_macro_in_all_wbks_in_fldr()
Dim fname As String, folder As String
folder = "C:/users/Kaper/test/"
fname = Dir(folder & "*.xl*")
While fname <> ""
  Workbooks.Open folder & fname ' the workbook from folder is opened and it is now active
    Call test
  ' may be you need: ActiveWorkbook.Save False ' here if the macro changes content
  ActiveWorkbook.Close 'so this closes the newly opened one
  fname = Dir
Wend
End Sub

Sub test()
  Debug.Print ActiveWorkbook.Name; ActiveWorkbook.Sheets.Count
End Sub

You may want to swith off screen refresh before loop
VBA Code:
Application.ScreenUpdating = False

and swith back on after
 
Upvote 0
hello thank for code but when I run it by F8 it wont open nothing what exactly should happen here

VBA Code:
fname = Dir(folder & "*.xl*") 'probalby here there is bad creation of exel name because nothing is opened
While fname <> ""
 
Upvote 0
hello thank for code but when I run it by F8 it wont open nothing what exactly should happen here

VBA Code:
fname = Dir(folder & "*.xl*") 'probalby here there is bad creation of exel name because nothing is opened
While fname <> ""
I got it I forgot last backslash in folder path
 
Upvote 0

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