PGSystemTester

Excel Macro Find More Than One Condition In a Cell

Jul 12th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Basic Macro to find a cell containing with more than one value. Posted on StackExchange in answer to below question:
  2. 'https://stackoverflow.com/questions/45069573/how-do-i-find-a-cell-that-contains-several-values/
  3.  
  4. 'I post all of my code for free and am not seeking any financial compensation. If you're
  5. 'compelled to be generous or find this helpful, please consider a tax deductible
  6. 'donation to Reason Magazine: https://reason.com/donatenow/donate.php
  7. 'Share a screenshot with me of your donation, even if just a few bucks.
  8. 'It inspires me to post more of these. My username is PGCodeRider on most social media.
  9.  
  10.  
  11.  
  12.     Sub FindLots()
  13.           Dim TextArray() As String, WS As Worksheet, Targetcell As Range
  14.           Dim Answer As String, StartingAddress As String
  15.           Dim AllSearchText As String, QuestionHeader As String
  16.           Dim I As Integer, t As Integer, NumericAnswer As Integer
  17.           Dim NoMemberFound As Boolean
  18.          
  19. StartQuestion:
  20.             If I = 0 Then
  21.                 QuestionHeader = "Enter Your Search Text"
  22.             Else
  23.                 QuestionHeader = "Enter Your Search... part " & I + 1 & " !"
  24.            
  25.             End If
  26.        
  27.             Answer = InputBox("Add a field to search for and hit ""OK."" You will get a chance to enter search fields.", QuestionHeader, "Enter Text")
  28.          
  29.             If Answer = "" Then
  30.               NumericAnswer = MsgBox("You didn't enter anything. Click ""Yes"" to try again. ""No"" to start search or ""Cancel"" to... cancel.", vbYesNoCancel, "Oh False!")
  31.            
  32.                 If NumericAnswer = vbYes Then
  33.                     GoTo StartQuestion
  34.                 ElseIf NumericAnswer = vbCancel Then
  35.                     Exit Sub
  36.                 End If
  37.             Else
  38.            
  39.                 ReDim Preserve TextArray(I)
  40.                 TextArray(I) = Answer
  41.                 AllSearchText = AllSearchText & "," & Answer
  42.                
  43.                 NumericAnswer = MsgBox("Would you like to add an additional members to search of """ & AllSearchText & """? Click no to continue search.", vbQuestion + vbYesNoCancel)
  44.                     If NumericAnswer = vbYes Then
  45.                         I = I + 1
  46.                         GoTo StartQuestion
  47.                     ElseIf NumericAnswer = vbCancel Then
  48.                         Exit Sub
  49.                     End If
  50.             End If
  51.        
  52.             On Error Resume Next
  53.             If TextArray(0) = "" Then
  54.                 MsgBox "No Search text entered", vbCritical
  55.                 Exit Sub
  56.             End If
  57.             On Error GoTo 0
  58.        
  59.        
  60.         Set WS = ActiveSheet 'or whatever sheet you want to search
  61.      
  62.         Set Targetcell = WS.Cells.Find(TextArray(0), WS.Cells(1, 1))
  63.        
  64.         If Targetcell Is Nothing Then
  65.             MsgBox "coulnd't even find " & TextArray(0), vbCritical
  66.             Exit Sub
  67.         ElseIf I = 0 Then
  68.             MsgBox "Found your cell at " & Targetcell.Address
  69.             Targetcell.Select
  70.             Exit Sub
  71.         End If
  72.        
  73.         StartingAddress = Targetcell.Address
  74.        
  75.         Do
  76.         NoMemberFound = False
  77.         For t = 1 To I
  78.        
  79.         If Targetcell.Cells.Find(TextArray(t)) Is Nothing Then
  80.             NoMemberFound = True
  81.             Exit For
  82.         End If
  83.         Next t
  84.        
  85.         If NoMemberFound = False Then
  86.             Dim NumberOfItemsFound As Integer
  87.                 NumberOfItemsFound = NumberOfItemsFound + 1
  88.                 Dim aReply As Integer: aReply = MsgBox("Found your cell at " & Targetcell.Address & ". Keep searching?", vbYesNo, "Yea!")
  89.                 Targetcell.Select
  90.                 If Not (aReply = vbYes) Then Exit Sub
  91.                
  92.         End If
  93.        
  94.         Set Targetcell = WS.Cells.Find(TextArray(0), Targetcell)
  95.        
  96.         Loop Until Targetcell.Address = StartingAddress
  97.        
  98.         If NumberOfItemsFound > 0 Then
  99.             MsgBox "Search completed. " & NumberOfItemsFound & " cells met your search requirement."
  100.         Else
  101.             MsgBox "Unable to find cells with your criteria of " & Right(AllSearchText, Len(AllSearchText) - 1), vbInformation, "Is that bad?"
  102.         End If
  103.        
  104.         End Sub
Add Comment
Please, Sign In to add comment