I am trying to write a short procedure that searches a database of files to find all records that match a search criteria that is listed in a column in my master workbook. I can on manage to search values in a sheet but not is directory of workbooks.
Option Explicit
Sub SearchMutipleTargets()
Dim datasheet As Worksheet 'where is the data copied from
Dim reportsheet As Worksheet 'where the data is pasted to
Dim athletename As String
Dim finalrow As Integer 'the last row of the data set
Dim i As Integer 'row counter for the loop
Dim t As Integer 'row count for the target names
Dim targetcount As Integer 'how many target names there are
'set variables
Set datasheet = Sheet2
Set reportsheet = Sheet1
'clear old data from report sheet - code clear down to row 1000
reportsheet.Range("A9:L1000").ClearContents
targetcount = Cells(7, 2).End(xlUp).Row
'go to datasheet and start searching and copying
datasheet.Select
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
'loop through the target names
For t = 1 To targetcount
athletename = reportsheet.Cells(t, 2).Value
'loop through the rows to find the matching records
For i = 2 To finalrow
If Cells(i, 2) = athletename Then
Range(Cells(i, 1), Cells(i, 12)).Copy
reportsheet.Select
Range("A1000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
datasheet.Select
End If
Next i
Next t
reportsheet.Select 'this is so that the report sheet is selected when the procedure ends
Range("B2").Select
MsgBox ("Search Complete")
End Sub