VBA - Substring position using String Masking

GlennL

New Member
Joined
Sep 25, 2018
Messages
37
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello,

Is there a simple way of finding a position of a substring within a string using a string-mask?

This is the original substring
06.12.2022 04:31:39 UTC

and I want to search for it using the mask
++.++.++++ ++:++:++ UTC

I could make a function which loops through the string and replaces the integers with the +, however, I thought there maybe an easier way. Is there?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Like this?
VBA Code:
If str Like "*??.??.???? ??:??:?? UTC*" Then
  'Do stuff
End If
 
Upvote 0
Something like this maybe:
VBA Code:
Sub Test()
  Dim str() As String
  Dim testStr As String
  
  testStr = Cells(1, 1).Value
  str = Split(testStr)
    For i = 0 To UBound(str) - 2
    For j = i + 1 To UBound(str) - 1
      For k = i + 1 To UBound(str)
        If str(i) & " " & str(j) & " " & str(k) Like "*??.??.???? ??:??:?? UTC*" Then
          MsgBox InStr(testStr, str(i) & " " & str(j) & " " & str(k))
        End If
      Next k
    Next j
  Next i
End Sub

1670312028980.png
 
Upvote 0
Solution
A slightly efficient version:
VBA Code:
Sub Test()
  Dim str() As String
  Dim testStr As String
 
  testStr = Cells(1, 1).Value
  str = Split(testStr)
  For i = 0 To UBound(str) - 2
    If str(i) & " " & str(i + 1) & " " & str(i + 2) Like "*??.??.???? ??:??:?? UTC*" Then
      MsgBox InStr(testStr, str(i) & " " & str(i + 1) & " " & str(i + 2))
    End If
  Next
End Sub
 
Upvote 0
You could also get the starting position of a regex-match;

VBA Code:
Sub Test()

Dim s As String: s = "Test 06.12.2022 04:31:39 UTC Test"
Debug.Print RegexMatchPos(s, "\d\d\.\d\d\.\d{4} \d\d:\d\d:\d\d UTC")

End Sub

Function RegexMatchPos(s_in As String, pat As String) As Long

With CreateObject("vbscript.regexp")
    .Pattern = pat
    If .Test(s_in) Then
        RegexMatchPos = .Execute(s_in)(0).FirstIndex + 1
    Else
        RegexMatchPos = 0
    End If

End With

End Function
 
Upvote 0
Another option could be to apply worksheet functionality:

VBA Code:
Sub Test()

Dim s As String: s = "Test 06.12.2022 04:31:39 UTC Test"
Debug.Print Application.Search("??.??.???? ??:??:?? UTC", s)

End Sub
 
Upvote 0
VBA

VBA Code:
Sub jec()
 Dim xStr
 xStr = "Test 06.12.2022 04:31:39 UTC Test"
 
 With CreateObject("vbscript.regexp")
   .Pattern = "\d+\..*\d+:.*UTC"
   If .Test(xStr) Then Debug.Print .Execute(xStr)(0).firstindex + 1
 End With
End Sub

Formula

Excel Formula:
=SEARCH("??.*UTC",A1)
 
Upvote 0

Forum statistics

Threads
1,224,523
Messages
6,179,301
Members
452,904
Latest member
CodeMasterX

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