Hello, my first post so I hope I am doing this correctly
First thanks for considering my question
I have asked this question on different forum about a week ago and had no meaningful response so I am trying a different forum
I have the Function below that finds the nth occurrence of a substring in a string and it works great, but it will not take wildcards
And I desperately need to use it with wildcards
Can anyone see how to alter the function or the way I am using it that will permit me to use it with wildcards?
I have tried but have been no luck
Again thanks
Here is a common example of what I want to do, I have a column of data that looks like this (again this is only an example of a common need)
Also in this example, the position of the substring is 1, but other times it can be 2, or 5 or n
I want it to become this:
So I need to remove this:
My Function
I am using it like this:
First thanks for considering my question
I have asked this question on different forum about a week ago and had no meaningful response so I am trying a different forum
I have the Function below that finds the nth occurrence of a substring in a string and it works great, but it will not take wildcards
And I desperately need to use it with wildcards
Can anyone see how to alter the function or the way I am using it that will permit me to use it with wildcards?
I have tried but have been no luck
Again thanks
Here is a common example of what I want to do, I have a column of data that looks like this (again this is only an example of a common need)
Also in this example, the position of the substring is 1, but other times it can be 2, or 5 or n
Code:
Some Col
What types of activities have you engaged in with [Field-3]? You can choose multiple answers. - What types of activities have you engaged in with [Field-3]? You can choose multiple answers. - Selected Choice
What types of activities have you engaged in with [Field-4]? You can choose multiple answers. - What types of activities have you engaged in with [Field-4]? You can choose multiple answers. - Selected Choice
What types of activities have you engaged in with [Field-5]? You can choose multiple answers. - What types of activities have you engaged in with [Field-5]? You can choose multiple answers. - Selected Choice
etc.
I want it to become this:
Code:
What types of activities have you engaged in with [Field-3]
What types of activities have you engaged in with [Field-4]
What types of activities have you engaged in with [Field-5]
etc.
So I need to remove this:
Code:
? You can choose multiple answers. - What types of activities have you engaged in with[*]? You can choose multiple answers. - Selected Choice
My Function
Code:
Function ReplaceN(ByVal str1 As Variant, strFind As String, strReplace As String, N As Long, Optional Count As Long) As String
Dim i As Long, j As Long
Dim strM As String
strM = str1
If Count <= 0 Then Count = 1
For i = 1 To N - 1
j = InStr(1, strM, strFind)
strM = Mid(strM, j + Len(strFind), Len(strM))
Next i
If N <= 0 Then
ReplaceN = str1
Else
ReplaceN = Mid(str1, 1, Len(str1) - Len(strM)) & Replace(strM, strFind, strReplace, Start:=1, Count:=Count)
End If
End Function
I am using it like this:
Code:
Sub ReplaceNthInstance()
Dim ws As Worksheet
Dim outArray As Variant
Dim i As Long, LR As Long
Set ws = ThisWorkbook.Sheets("Z")
With ws.Range("E:E")
With Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
outArray = .value
For i = 1 To .Rows.Count
outArray(i, 1) = ReplaceN(.Cells(i, 1).value, "[*]", vbNullString, 1)
Next i
.value = outArray
End With
End With
End Sub
Last edited: