• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Applying specific macro to specific file in a folder

soft_reg

New Member
Hi All,

I'm a novice just started Excel Macros on Mar 1, 2016. I request your help to figure out the below problem. I very much respect and appreciate the work you guys do here.

I've three files namely Proximus.xlsx, Mobistar.xlsx and Telenet.xlsx. Each of them have one worksheet named Carrier_draft having thousands of rows & around 100 columns with data in different format, I've written Macros (MacroProximus, MacroMobistar and MacroTelenet to format the data in them.
How do I go to folder c:\Carriers\Draft and apply MacroProximus to Proximus.xlsx, MacroMobistar to Mobistar.xlsx and MacroTelenet to Telenet.xlsx ?

I have this code working for one file *Proximus*.xlsx but I need it to work on the other two files as well, the three files get updated every day with datestamp like Proximus04022016, Mobistar04022016. How I can modify the below code to search filename like Proximus*.xlsx ?

Code:
Sub Loopfiles()
Dim wb As Workbook
Dim myPath As String
Dim myfile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual

'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

  With FldrPicker
  .Title = "Select A Target Folder"
  .AllowMultiSelect = False
  If .Show <> -1 Then GoTo NextCode
  myPath = .SelectedItems(1) & "\"
  End With
'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Target File Extension (must include wildcard "*")
  myExtension = "*Proximus*.xlsx"

'Target Path with Ending Extention
  myfile = Dir(myPath & myExtension)

'Loop through each Excel file in folder
  Do While myfile <> ""
  'Set variable equal to opened workbook
  Set wb = Workbooks.Open(Filename:=myPath & myfile)
  Call MacroProximus
  wb.Close SaveChanges:=True

  'Get next file name
  myfile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True

End Sub
 
Back
Top