loop through files with certain name

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
707
Office Version
  1. 365
  2. 2010
hi,

trying to make a macro to look through certain files like "*[ac]_greek_Analysis.xls"


then open each file....if each file has a certain value in range("c10") ...then save the file name according to that file

im currently using this code below, but it's clearly not efficient as im essentially repeating the code 11 times .... (only thing that im changing is the beginning part of file name...so instead of "1_[AC]...." ...changing it to "2_[AC]"
Code:
'first file
  Set wb1 = Workbooks.Open(ThisWorkbook.Path & "\1_[AC]_greek_Analysis.xls")




Select Case Range("c10").Value = "12345"


Case True


 ActiveWorkbook.SaveAs Filename:= _
         ThisWorkbook.Path & "\12345.xls"


ActiveWorkbook.Close True




Case Else




    Select Case Range("c10").Value = "abcde"


Case True
 ActiveWorkbook.SaveAs Filename:= _
         ThisWorkbook.Path & "\abcde.xls"


ActiveWorkbook.Close True




Case Else


    Select Case Range("c10").Value = "456789"


Case True
 ActiveWorkbook.SaveAs Filename:= _
         ThisWorkbook.Path & "\456789.xls"


ActiveWorkbook.Close True


etc. etc.
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try something like this (note: not tested):
Code:
    For i = 1 To 12 Step 1
        Set wb1 = Workbooks.Open(ThisWorkbook.Path & "\" & i & "_[AC]_greek_Analysis.xls")
        ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Range("C10").Value & ".xls"
        ActiveWorkbook.Close True
    Next i
 
Upvote 0

Forum statistics

Threads
1,215,016
Messages
6,122,700
Members
449,092
Latest member
snoom82

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