I created a function which will go through every cell in column B to replace "bad" strings with "good" strings. Below is what I have come up with.
My problem is that the Replace and InStr functions do not seem to work. My variable strPos is always 0. Replace never replaces the string even when I remove the line "If strPos > 0 Then".
Can anyone help?
Dim strBadString, strGoodString As String
Dim badStringArray() As String
Dim goodStringArray() As String
Dim strURL As String
Dim strPos As Integer
Dim RowCount As Long
strBadString = "//pwgsc-tpsgc.com, .pwgsc-tpsgc., .tpsgc., //w3."
strGoodString = "// .pwgsc., .pwgsc., //
badStringArray = Split(strBadString, ",")
goodStringArray = Split(strGoodString, ",")
With Worksheets(1)
RowCount = .UsedRange.Rows.Count
End With
For i = 1 To RowCount
strURL = Worksheets(1).Cells(i, "B").Value
For x = 0 To UBound(badStringArray)
strPos = InStr(strURL, badStringArray(x))
If strPos > 0 Then
strURL = Replace(strURL, badStringArray(x), goodStringArray(x))
Worksheets(1).Cells(i, "B").Value = strURL
End If
strPos = 0
Next x
Next i
My problem is that the Replace and InStr functions do not seem to work. My variable strPos is always 0. Replace never replaces the string even when I remove the line "If strPos > 0 Then".
Can anyone help?
Dim strBadString, strGoodString As String
Dim badStringArray() As String
Dim goodStringArray() As String
Dim strURL As String
Dim strPos As Integer
Dim RowCount As Long
strBadString = "//pwgsc-tpsgc.com, .pwgsc-tpsgc., .tpsgc., //w3."
strGoodString = "// .pwgsc., .pwgsc., //
badStringArray = Split(strBadString, ",")
goodStringArray = Split(strGoodString, ",")
With Worksheets(1)
RowCount = .UsedRange.Rows.Count
End With
For i = 1 To RowCount
strURL = Worksheets(1).Cells(i, "B").Value
For x = 0 To UBound(badStringArray)
strPos = InStr(strURL, badStringArray(x))
If strPos > 0 Then
strURL = Replace(strURL, badStringArray(x), goodStringArray(x))
Worksheets(1).Cells(i, "B").Value = strURL
End If
strPos = 0
Next x
Next i