Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub SearchWB()
- Dim myDir As String, fn As String, ws As Worksheet, r As Range
- Dim a(), n As Long, x As Long, myTask As String, ff As String, temp
- myDir = "C:test" '<- change path to folder with files to search
- If Dir(myDir, 16) = "" Then
- MsgBox "No such folder path", 64, myDir
- Exit Sub
- End If
- myTask = InputBox("Enter Customer Name")
- If myTask = "" Then Exit Sub
- x = Columns.Count
- fn = Dir(myDir & "*.xls*")
- With Application
- .ScreenUpdating = False
- .EnableEvents = False
- End With
- Do While fn <> ""
- With Workbooks.Open(myDir & fn, 0)
- For Each ws In .Worksheets
- Set r = ws.Cells.Find(myTask, , , 1)
- If Not r Is Nothing Then
- ff = r.Address
- Do
- n = n + 1
- temp = r.EntireRow.Value
- ReDim Preserve temp(1 To 1, 1 To x)
- ReDim Preserve a(1 To n)
- a(n) = temp
- Set r = ws.Cells.FindNext(r)
- Loop While ff <> r.Address
- End If
- Next
- .Close False
- End With
- fn = Dir
- Loop
- With ThisWorkbook.Sheets(1).Rows(1)
- .CurrentRegion.ClearContents
- If n > 0 Then
- .Resize(n).Value = _
- Application.Transpose(Application.Transpose(a))
- Else
- MsgBox "Not found", , myTask
- End If
- End With
- End Sub
- Sub SearchWB()
- Dim myDir As String, fn As String, ws As Worksheet, ws2 As Worksheet, r As Range
- Dim a(), b(), n As Long, x As Long, f As Long, myTask As String, ff As String, temp, temp2
- myDir = "C:UserscaraltojDesktopfitxes inspeccio1" '<- change path to folder with files to search
- If Dir(myDir, 16) = "" Then
- MsgBox "No such folder path", 64, myDir
- Exit Sub
- End If
- myTask = InputBox("buscar")
- If myTask = "" Then Exit Sub
- x = Columns.Count
- fn = Dir(myDir & "*.xls*")
- With Application
- .ScreenUpdating = False
- .EnableEvents = False
- End With
- Do While fn <> ""
- With Workbooks.Open(myDir & fn, 0)
- Set ws = .Worksheets(1)
- Set ws2 = .Worksheets(2)
- Set r = ws.Cells.Find(myTask, , , 1)
- If Not r Is Nothing Then
- ff = r.Address
- Do
- f = f + 1
- n = n + 1
- temp = r.EntireRow.Value
- temp2 = Dir(myDir & "*.xls*")
- ReDim Preserve temp(1 To 1, 1 To x)
- ReDim Preserve a(1 To n)
- ReDim Preserve f(f)
- a(n) = temp
- b(f) = temp2
- Set r = ws.Cells.FindNext(r)
- Loop While ff <> r.Address
- End If
- .Close False
- End With
- fn = Dir
- Loop
- With ThisWorkbook.Sheets(1).Rows(1)
- .CurrentRegion.ClearContents
- If n > 0 Then
- .Resize(n).Value = _
- Application.Transpose(Application.Transpose(a))
- Else
- MsgBox "Not found", , myTask
- End If
- End With
- With ThisWorkbook.Sheets(2).Rows(1)
- .CurrentRegion.ClearContents
- If f > 0 Then
- .Resize(f).Value = _
- Application.Transpose(Application.Transpose(b))
- Else
- MsgBox "Not found", , myTask
- End If
- End With
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement