macro to open specific Files in a folder

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have tried to write code to open all files in C:\Fixed assets that contain fixed assets for eg BR1 Fixed assets.xlsm, Souther Fixed Assets.xlsm etc

Code:
 Sub Open_files()
ChDir "C:\Fixed Assets"
Dim A As Variant
Dim LR As Long


If TypeName(A) = "Boolean" Then Exit Sub


Dim FileAry As Variant, Fle As Variant
   With Application.FileDialog(3)
      .InitialFileName = "C:\Fixed Assets\"
      .AllowMultiSelect = True
      .InitialFileName = "*Fixed*Assets*.xls*"
      If .Show Then Set FileAry = .SelectedItems()
   End With
   For Each File In FileAry
   If TypeName(A) = "Boolean" Then Exit Sub

   
      With Workbooks.Open(File)
                   
   End With

I would like this amended so that it excludes workbooks containing "Consolidated" and "Email"



Application.ScreenUpdating = True
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this:

VBA Code:
Sub Open_FixedAssets()
  Dim sFile As String
  Dim sPath As String
  
  sPath = "C:\Fixed Assets\"
  sFile = Dir(sPath & "*Fixed Assets*.xls*")
  
  Do While sFile <> ""
    Workbooks.Open sPath & sFile
    sFile = Dir()
  Loop
End Sub
 
Upvote 0
Thanks Dante

Kindly amend code to exclude workbooks containing "Consolidated" and "Email" per my post # 1
 
Upvote 0
Try this:

VBA Code:
Sub Open_files()
  Dim FileAry As Variant, File As Variant
  Dim A As Variant
  
  ChDir "C:\Fixed Assets"
  If TypeName(A) = "Boolean" Then Exit Sub
  
  With Application.FileDialog(3)
    .InitialFileName = "C:\Fixed Assets\"
    .AllowMultiSelect = True
    .InitialFileName = "*Fixed*Assets*.xls*"
    If .Show Then Set FileAry = .SelectedItems()
  End With
  
  Application.ScreenUpdating = False
  For Each File In FileAry
    If TypeName(A) = "Boolean" Then Exit Sub
    If InStr(1, File, "Consolidated", vbTextCompare) = 0 And _
       InStr(1, File, "Email", vbTextCompare) = 0 Then
      With Workbooks.Open(File)
    
      End With
    End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,881
Messages
6,122,074
Members
449,064
Latest member
MattDRT

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