Custom variation of basic string is within a string function.

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
855
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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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