Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function FindAll(SearchRange As Range, _
  2.                 FindWhat As Variant, _
  3.                Optional LookIn As XlFindLookIn = xlValues, _
  4.                 Optional LookAt As XlLookAt = xlWhole, _
  5.                 Optional SearchOrder As XlSearchOrder = xlByRows, _
  6.                 Optional MatchCase As Boolean = False, _
  7.                 Optional BeginsWith As String = vbNullString, _
  8.                 Optional EndsWith As String = vbNullString, _
  9.                 Optional BeginEndCompare As VbCompareMethod = vbTextCompare) As Range
  10. Dim foundCell As Range
  11. Dim FirstFound As Range
  12. Dim LastCell As Range
  13. Dim ResultRange As Range
  14. Dim XLookAt As XlLookAt
  15. Dim Include As Boolean
  16. Dim CompMode As VbCompareMethod
  17. Dim Area As Range
  18. Dim MaxRow As Long
  19. Dim MaxCol As Long
  20.  
  21.  
  22. CompMode = BeginEndCompare
  23. If BeginsWith <> vbNullString Or EndsWith <> vbNullString Then
  24.     XLookAt = xlPart
  25. Else
  26.     XLookAt = LookAt
  27. End If
  28.  
  29. For Each Area In SearchRange.Areas
  30.     With Area
  31.         If .Cells(.Cells.count).Row > MaxRow Then
  32.             MaxRow = .Cells(.Cells.count).Row
  33.         End If
  34.         If .Cells(.Cells.count).Column > MaxCol Then
  35.             MaxCol = .Cells(.Cells.count).Column
  36.         End If
  37.     End With
  38. Next Area
  39. Set LastCell = SearchRange.Worksheet.Cells(MaxRow, MaxCol)
  40.  
  41.  
  42. On Error GoTo 0
  43. Set foundCell = SearchRange.Find(What:=FindWhat, _
  44.         After:=LastCell, _
  45.         LookIn:=LookIn, _
  46.         LookAt:=xlPart, _
  47.         SearchOrder:=SearchOrder, _
  48.         MatchCase:=MatchCase)
  49.  
  50. If Not foundCell Is Nothing Then
  51.     Set FirstFound = foundCell
  52.     Set ResultRange = foundCell
  53.     Set foundCell = SearchRange.FindNext(After:=foundCell)
  54.     Do Until False ' Loop forever. We'll "Exit Do" when necessary.
  55.        If (foundCell Is Nothing) Then
  56.             Exit Do
  57.         End If
  58.         If (foundCell.Address = FirstFound.Address) Then
  59.             Exit Do
  60.         End If
  61.         Include = False
  62.            
  63.         If BeginsWith = vbNullString Then
  64.             If EndsWith = vbNullString Then
  65.                 Include = True
  66.             Else
  67.                 If Len(foundCell.text) < Len(EndsWith) Then
  68.                     Include = False
  69.                 Else
  70.                     If StrComp(Right(foundCell.text, Len(EndsWith)), EndsWith, CompMode) = 0 Then
  71.                         Include = True
  72.                     Else
  73.                         Include = False
  74.                     End If
  75.                 End If
  76.             End If
  77.         End If
  78.         If EndsWith = vbNullString Then
  79.             If BeginsWith = vbNullString Then
  80.                 Include = True
  81.             Else
  82.                 If StrComp(Left(foundCell.text, Len(BeginsWith)), BeginsWith, CompMode) = 0 Then
  83.                     Include = True
  84.                 Else
  85.                     Include = False
  86.                 End If
  87.             End If
  88.         Else
  89.             If Len(foundCell.text) < Len(EndsWith) Then
  90.                 Include = False
  91.             Else
  92.                 If StrComp(Right(foundCell.text, Len(EndsWith)), EndsWith, CompMode) = 0 Then
  93.                     Include = True
  94.                 Else
  95.                     Include = False
  96.                 End If
  97.             End If
  98.         End If
  99.        
  100.         If Include = True Then
  101.             Set ResultRange = Application.Union(ResultRange, foundCell)
  102.         End If
  103.         Set foundCell = SearchRange.FindNext(After:=foundCell)
  104.     Loop
  105. End If
  106.    
  107. Set FindAll = ResultRange
  108.  
  109. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement