OaklandJim
Well-known Member
- Joined
- Nov 29, 2018
- Messages
- 855
- Office Version
- 365
- Platform
- 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.
I hate zero base of arrays so I use this to make arrays 1 base
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