I'm trying to find all the positions of the unique keys (followed by two Tab keystrokes) in a string in taken from clipboard, positions with which I then hope to use to insert carriage returns, and then have everything put back into the clipboard again.
First things first; getting the position part to work!
Here is a shortened example of the string:
Initial Approval in First Market or Non-Submitted Closure 090052fb842ef82f 090052fb842f3659 090052fb842ef82e
Here is the non-functional code I have put together so far from researching the problem:
Sub oldRecords()
Dim clipboard As MSForms.DataObject
Dim strContents As String
Set clipboard = New MSForms.DataObject
clipboard.GetFromClipboard
strContents = clipboard.GetText
Set objRegEx = CreateObject("vbscript.regexp")
objRegEx.Pattern = "(090052fb)[0-9A-Za-z]{8}\t\t"
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
Start = 1
Do
pos = InStr(Start, strContents, objRegEx.Execute(strContents), vbBinaryCompare)
If pos > 0 Then
Start = pos + Len(objRegEx.Pattern)
WScript.Echo pos
WScript.Echo Mid(strContents, pos, Len(objRegEx.Pattern))
End If
Loop While pos > 0
End Sub
Right now I am getting a Run-time error '450': Wrong number of arguments or invalid property assignment, and I believe the culprit is:
objRegEx.Execute(strContents)
I'm not sure where to go from here, so any help would be fantastic! :)
Edit 1: Firstly thank you for the interest in my issue!
BrackNicku has provided a simple solution for a problem I evidently thought more complex than it needed to be! Here is the code I finally went with, adding in a few extra bits that I needed on top of the core issue:
Sub oldRecords2()
Dim clipboard As MSForms.DataObject
Dim strContents As String
Dim start As Long, pos As Long
Set clipboard = New MSForms.DataObject
clipboard.GetFromClipboard
strContents = clipboard.GetText
Dim objRegEx
Set objRegEx = CreateObject("vbscript.regexp")
objRegEx.Pattern = "(090052fb[0-9A-Za-z]{8})\t\t"
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.MultiLine = True
X1 = 10 ' Line Feed Character
X2 = 13 ' Carriage Return Character
X3 = "Archive Custodain Group"
X4 = "Archive Custodain Group" & Chr(X2)
'======================================================================================================
strContents = Replace(strContents, Chr(X1), "") ' REMOVES LINE FEEDS
strContents = Replace(strContents, X3, X4) ' ADDS CR AFTER TITLE ROW
strContents = objRegEx.Replace(strContents, "$1" & vbNewLine)
'======================================================================================================
clipboard.SetText strContents 'PUT BACK INTO CLIPBOARD
clipboard.PutInClipboard
End Sub
pos + Len(objRegEx.Pattern)is wrong, as you add the length of the pattern, and not the match. Probably you need to find the match itself and get its index.59,77and95, is that expected? Tested againststrContents = "Initial Approval in First Market or Non-Submitted Closure" & vbTab & vbTab & "090052fb842ef82f" & vbTab & vbTab & "090052fb842f3659" & vbTab & vbTab & "090052fb842ef82e" & vbTab & vbTab090052fb842ef82f,090052fb842f3659,090052fb842ef82e.