Custom variation of basic string is within a string function.

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.

The logic for the function needed is a bit screwy but who can argue with someone posting about their need. I'll try to describe what I am after then give examples. I need to determine if a "partial" string is within another "full" string. Easy enough normally using InStr. BUT the twist is that if only a PART of the partial string is preset in the full string then the function should return false.

Example 1: Full string "stuff happy sad bloom". Partial string "happy sa" yields a False result because the partial string (as-is) is not present in the full string.

Example 2: Full string "stuff happy sad bloom". Partial string "happy sad" would yield True result because the entire partial string is present within the full string.

Example 3: Full string "stuff happy sad bloom". Partial string "stuff happy" would yield a True result because the entire partial string is included in the longer string.

Example 4: Full string "stuff happy sad bloom". Partial string "hap" would be a False result because the entire partial string (as is) is not present in the longer string.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Maybe with a UDF
VBA Code:
Function SComp(A As String, B As String) As Boolean
    Dim WS As Worksheet
    Dim I As Long, SPos As Long, WCnt As Long
    Dim S As String, TmpStr As String
    Dim SA As Variant
   
    Set WS = ActiveSheet
    SPos = InStr(A, B)
    If SPos > 0 Then
        SA = Split(B, " ")
        WCnt = UBound(SA) + 1
       
        TmpStr = Mid(A, SPos, Len(A))
        SA = Split(TmpStr, " ")
        For I = 0 To WCnt - 1
            S = S & SA(I) & " "
        Next I
        S = Trim(S)
        SComp = S = B
    Else
        SComp = False
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,215,124
Messages
6,123,187
Members
449,090
Latest member
bes000

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