Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. Sub SearchWB()
  2. Dim myDir As String, fn As String, ws As Worksheet, r As Range
  3. Dim a(), n As Long, x As Long, myTask As String, ff As String, temp
  4. myDir = "C:test" '<- change path to folder with files to search
  5. If Dir(myDir, 16) = "" Then
  6. MsgBox "No such folder path", 64, myDir
  7. Exit Sub
  8. End If
  9. myTask = InputBox("Enter Customer Name")
  10. If myTask = "" Then Exit Sub
  11. x = Columns.Count
  12. fn = Dir(myDir & "*.xls*")
  13. With Application
  14. .ScreenUpdating = False
  15. .EnableEvents = False
  16. End With
  17. Do While fn <> ""
  18. With Workbooks.Open(myDir & fn, 0)
  19. For Each ws In .Worksheets
  20. Set r = ws.Cells.Find(myTask, , , 1)
  21. If Not r Is Nothing Then
  22. ff = r.Address
  23. Do
  24. n = n + 1
  25. temp = r.EntireRow.Value
  26. ReDim Preserve temp(1 To 1, 1 To x)
  27. ReDim Preserve a(1 To n)
  28. a(n) = temp
  29. Set r = ws.Cells.FindNext(r)
  30. Loop While ff <> r.Address
  31. End If
  32. Next
  33. .Close False
  34. End With
  35. fn = Dir
  36. Loop
  37. With ThisWorkbook.Sheets(1).Rows(1)
  38. .CurrentRegion.ClearContents
  39. If n > 0 Then
  40. .Resize(n).Value = _
  41. Application.Transpose(Application.Transpose(a))
  42. Else
  43. MsgBox "Not found", , myTask
  44. End If
  45. End With
  46. End Sub
  47.  
  48. Sub SearchWB()
  49. Dim myDir As String, fn As String, ws As Worksheet, ws2 As Worksheet, r As Range
  50. Dim a(), b(), n As Long, x As Long, f As Long, myTask As String, ff As String, temp, temp2
  51. myDir = "C:UserscaraltojDesktopfitxes inspeccio1" '<- change path to folder with files to search
  52. If Dir(myDir, 16) = "" Then
  53. MsgBox "No such folder path", 64, myDir
  54. Exit Sub
  55. End If
  56. myTask = InputBox("buscar")
  57. If myTask = "" Then Exit Sub
  58. x = Columns.Count
  59. fn = Dir(myDir & "*.xls*")
  60. With Application
  61. .ScreenUpdating = False
  62. .EnableEvents = False
  63. End With
  64. Do While fn <> ""
  65. With Workbooks.Open(myDir & fn, 0)
  66. Set ws = .Worksheets(1)
  67. Set ws2 = .Worksheets(2)
  68. Set r = ws.Cells.Find(myTask, , , 1)
  69. If Not r Is Nothing Then
  70. ff = r.Address
  71. Do
  72. f = f + 1
  73. n = n + 1
  74. temp = r.EntireRow.Value
  75. temp2 = Dir(myDir & "*.xls*")
  76. ReDim Preserve temp(1 To 1, 1 To x)
  77. ReDim Preserve a(1 To n)
  78. ReDim Preserve f(f)
  79. a(n) = temp
  80. b(f) = temp2
  81. Set r = ws.Cells.FindNext(r)
  82.  
  83. Loop While ff <> r.Address
  84. End If
  85.  
  86. .Close False
  87. End With
  88. fn = Dir
  89. Loop
  90. With ThisWorkbook.Sheets(1).Rows(1)
  91. .CurrentRegion.ClearContents
  92. If n > 0 Then
  93. .Resize(n).Value = _
  94. Application.Transpose(Application.Transpose(a))
  95. Else
  96. MsgBox "Not found", , myTask
  97. End If
  98. End With
  99. With ThisWorkbook.Sheets(2).Rows(1)
  100. .CurrentRegion.ClearContents
  101. If f > 0 Then
  102. .Resize(f).Value = _
  103. Application.Transpose(Application.Transpose(b))
  104. Else
  105. MsgBox "Not found", , myTask
  106. End If
  107. End With
  108. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement