Logic puzzle? Determine if words in "find string" are in the same order as words in a "search string".

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
Office Version
  1. 365
Platform
  1. Windows
Am trying to help someone who posted to the list.

I need to determine if words in a find string (a single string with one or more words separated by space) are in the same order as words in a search string (also a single string with one or more words separated by space). I've got code that comes before that determination (see below) so you don't have to bother with that. I just need some help with the logic to make the determination. Results are not case sensitive.

Example 1: a) Search string "Goat Dog Cat Bear" b) Find string "Dog Cat" yields a True result.

Example 2: a) Search string "Goat Dog Cat Bear" b) Find string "Cat Bear" yields a True result.

Example 3: a) Search string "Goat Dog Cat Bear" b) Find string "Cat Dog" yields a False result.

Example 4: a) Search string "Goat Dog Cat Bear" b) Find string "Goat Bear" yields a False result.

VBA Code:
' ----------------------------------------------------------------
' Procedure Name: IsOutOfOrder
' Purpose: Determine if words in find string are not in same order as search string.
' Procedure Kind: Function
' Procedure Access: Public
' Parameter psSearchString (String): String of words to search in.
' Parameter psFindString (String): String of words to search for.
' Author: Jim
' Date: 5/9/2023
' ----------------------------------------------------------------

Function IsOutOfOrder(psSearchString As String, psFindString As String)

'   Array holding words in the search string.
    Dim asSearchWords() As String

'   Array holding words in the find string.
    Dim asFindWords() As String
    
'   Count of search words found and of find words found.
    Dim iSearchWordsCount As Long
    Dim iFindWordsCount As Long
    
'   Used to loop through words to process ("find words" present)
    Dim iFindWord As Long
    
'   Flag indicating that all words in find string are present in search string.
    Dim bAllWordsArePresent As Boolean
    
'   Split "search in" string and "find string" into arrays.
    asSearchWords = Split(psSearchString, " ")
    asFindWords = Split(psFindString, " ")
    
'   Convert split search words array and find words array to one based
'   indexing from zero based
    Call OneBasedArray(asSearchWords)
    Call OneBasedArray(asFindWords)
    
'   Count of search words found and of find words found.
    iSearchWordsCount = UBound(asSearchWords)
    iFindWordsCount = UBound(asFindWords)
    
'   Initialize the function return value.
    IsOutOfOrder = False
    
'   Determine if all words in string to find are in the search string.
    bAllWordsArePresent = IsAllWordsPresent(psSearchString, psFindString)

'   If All words are present then test for find words are out of order
'   relative to search words.
    If bAllWordsArePresent _
     Then
        
Debug.Print "here"

     End If
    
End Function

I hate zero base of arrays so I use this to make arrays 1 base
VBA Code:
' ----------------------------------------------------------------
' Procedure Name: OneBasedArray
' Purpose: Convert string array from zero based indexing to one based.
' Procedure Kind: Function
' Procedure Access: Public
' Parameter pasValues (String): Array to convert.
' Return Type: String)
' Author: Jim
' Date: 5/9/2023
' ----------------------------------------------------------------
Function OneBasedArray(ByRef pasValues() As String)
    
    Dim iUbound As Long
    
    Dim asTemp() As String
    
    Dim iValue As Long
    
    iUbound = UBound(pasValues)

    ReDim asTemp(1 To iUbound + 1)
    
    For iValue = 0 To iUbound
    
       asTemp(iValue + 1) = pasValues(iValue)
    
    Next iValue
    
    pasValues = asTemp
    
End Function

VBA Code:
' ----------------------------------------------------------------
' Procedure Name: IsAllWordsPresent
' Purpose: Determin if all words in a "find string" are within a "search string".
' Procedure Kind: Function
' Procedure Access: Public
' Parameter psSearchString (String): String to search in.
' Parameter psFindString (String): String containing words to search for.
' Return Type: Boolean
' Author: Jim
' Date: 5/9/2023
' ----------------------------------------------------------------

Function IsAllWordsPresent(psSearchString As String, psFindString As String) As Boolean

'   Used to keep track of whether a find word was found in search words.
    Dim abWordIsPresent() As Boolean

'   Array that holds words to find.
    Dim asFindWords() As String
    
'   Count of find words are present in the string psFindString.
    Dim iFindWordsCount As Long
    
'   Used to loop through words in string psFindString.
    Dim iFindWord As Long
    
'   Initialize the function return value.
    IsAllWordsPresent = True
    
'   Put all words to find in array asFindWords
    asFindWords = Split(psFindString, " ")
    
'   Make the split array one based instead of zero based.
    Call OneBasedArray(asFindWords)
    
'   Count of "find words" that are in the asFindWords array.
    iFindWordsCount = UBound(asFindWords)
    
'   Size the array holding true/false (found/not found) flags
    ReDim abWordIsPresent(iFindWordsCount)
    
'   Search in for all find words to determine if all find words are present.
    For iFindWord = 1 To iFindWordsCount

        If InStr(UCase(psSearchString), UCase(asFindWords(iFindWord))) _
         Then
            abWordIsPresent(iFindWord) = True
        Else
            abWordIsPresent(iFindWord) = False
        End If

    Next iFindWord

'   Determine if all find words are present in search words.
    For iFindWord = 1 To iFindWordsCount

        If Not abWordIsPresent(iFindWord) _
         Then
            IsAllWordsPresent = False
            Exit For
        End If

    Next iFindWord

End Function
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I think I figured it out. As usual, don't make coding more difficult than it already is!

VBA Code:
Option Explicit

' ----------------------------------------------------------------
' Procedure Name: IsOutOfOrder
' Purpose: Determine if words in find string are not in same order as search string.
' Procedure Kind: Function
' Procedure Access: Public
' Parameter psSearchString (String): String of words to search in.
' Parameter psFindString (String): String of words to search for.
' Author: Jim
' Date: 5/9/2023
' ----------------------------------------------------------------

Function IsOutOfOrder(psSearchString As String, psFindString As String)

'   Array holding words in the search string.
    Dim asSearchWords() As String

'   Array holding words in the find string.
    Dim asFindWords() As String
   
'   Count of search words found and of find words found.
    Dim iSearchWordsCount As Long
    Dim iFindWordsCount As Long
   
'   Used to loop through words to process ("find words" present)
    Dim iFindWord As Long
   
'   Flag indicating that all words in find string are present in search string.
    Dim bAllWordsArePresent As Boolean
   
'   Split "search in" string and "find string" into arrays.
    asSearchWords = Split(psSearchString, " ")
    asFindWords = Split(psFindString, " ")
   
'   Convert split search words array and find words array to one based
'   indexing from zero based
    Call OneBasedArray(asSearchWords)
    Call OneBasedArray(asFindWords)
   
'   Count of search words found and of find words found.
    iSearchWordsCount = UBound(asSearchWords)
    iFindWordsCount = UBound(asFindWords)
   
'   Initialize the function return value.
    IsOutOfOrder = True
   
'   Determine if all words in string to find are in the search string.
    bAllWordsArePresent = IsAllWordsPresent(psSearchString, psFindString)

'   If All words are present then test for find words are out of order
'   relative to search words.
    If bAllWordsArePresent _
     Then
       
        If InStr(1, UCase(psSearchString), UCase(psFindString)) _
         Then
            IsOutOfOrder = False
        Else
            IsOutOfOrder = True
        End If
   
    Else
        IsOutOfOrder = False
   
    End If
   
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,214,651
Messages
6,120,742
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top