I am trying to search all excel workbooks in a folder (and sub folders) for a value.
My folder structure where my excel workbooks are is like so:
destPath = "G:WH DISPO(3) PROMOTIONS(18) Food Specials Delivery TrackingArchive"
Then within my archive folder there are various sub folders like
+ 2017
- April
- May
+ 2016
- April
- May
The names of the workbooks might all be different, so the code will need to probably use something like wildcards *.xlsm
Here's what i have so far:
Sub Search()
Dim srcWorkbook As Workbook
Dim destWorkbook As Workbook
Dim srcWorksheet As Worksheet
Dim destWorksheet As Worksheet
Dim SearchRange As Range
Dim destPath As String
Dim destname As String
Dim destsheet As String
Set srcWorkbook = ActiveWorkbook
Set srcWorksheet = ActiveSheet
Dim vnt_Input As String
vnt_Input = Application.InputBox("Please Enter Client Name", "Client Name")
destPath = "G:WH DISPO(3) PROMOTIONS(18) Food Specials Delivery TrackingArchive"
destname = "*.xlsm"
On Error Resume Next
Set destWorkbook = ThisWorkbook
If Err.Number <> 0 Then
Err.Clear
Set wbTarget = Workbooks.Open(destPath & destname)
CloseIt = True
End If
For Each c In wbTarget.Sheets(1).Range("A:Q") 'No need for the .Cells here
If InStr(c, vnt_Input) > 0 Then 'vnt_Input is a variable that holds a string, so you can't put quotes around it, or it will search the string for "vnt_Input"
MsgBox "Found"
End If
Next c
End Sub
The ranges in each workbook should always stay the same.
I am trying something simple, like display a message when the value is found. But at the moment, despite the value existing in the workbook i am getting no result/no message.
I get an object required error on this line:
For Each c In wbTarget.Sheets(1).Range("A:Q") 'No need for the .Cells here
Please can someone show me where i am going wrong?
EDIT:
Can i change the message box to a for each loop to list each result like so:
Dim i As Integer
For i = 20 To 100
For Each rngFound In rngFound
ThisWorkbook.ActiveSheet.Range("E" & i).Value = "1 Result found for " & rngFound & " in " & wbTarget.Path & "" & wbTarget.Name & ", on row " & rngFound.Address
Next rngFound
Next i
Desired Result
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…