I need a macro to open all files in a folder that have the value in a cell somewhere in their filename. The files will be opened and closed one by one to copy some info and paste into the active workbook. Here's something to give you an idea what I'm after:
Sub test2()
Dim bk As Workbook
Dim Criteria1 As String
Dim Criteria2 As String
Dim Criteria3 As String
Dim Criteria As String
Dim path As String
path = ("C:\Documents and Settings\" & Environ("username") & "\Desktop\fiscal reports")
Criteria1 = "*" & Sheets("console").Range("E8").Value & " " & Sheets("console").Range("F4").Value & "*" 'for all rep, entire program year
Criteria2 = Sheets("console").Range("E8").Value & "*" & Sheets("console").Range("F4").Value & "*" 'for all agency, program entire year
Criteria3 = "*" & Sheets("console").Range("F4").Value & "*" 'for all reports (all agency and all rep), entire program year (all reps or all agencies)
If Sheets("console").Range("E6").Value = "Agency" Then
Criteria = Criteria2
End If
If Sheets("console").Range("E6").Value = "Representative" Then
Criteria = Criteria1
End If
If Sheets("console").Range("E6").Value = "All*" Then
Criteria = Criteria3
End If
'loop through all the books in the folder that meet the name criteria above
For Each bk In path
If bk.Name Like Criteria Then
bk.Open
'perform some actions, then close the workbook and open the next one
Activeworkbook.Close
Exit For
End If
Next
End Sub
Bookmarks