Find and replace text

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
I am trying to use the following script to search through a cells text and replace any invalid characters with "_". This script will identify the invalid characters but it will not replace the text. What am I doing wrong here?

Code:
Sub test()
Dim i As Integer
Dim Pos As Integer
Dim SearchString As String
Dim correction As String
 
SearchString = Range("A1").Value

FindChar = Array(":", "/", " ")

For Each c In FindChar
For i = 1 To Len(SearchString)
    If Mid(SearchString, i, 1) = c Then
        Pos = i
        correction = Replace(SearchString, i, "_")
        Range("A1").Value = correction
    End If
Next i
If Pos <> 0 Then
MsgBox Chr(34) & c & Chr(34) & " was Found at position " & Pos
End If
Pos = 0
Next c
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
in the Replace function the 2nd argument should be a string to find, not its position.
 
Last edited:
Upvote 0
Code:
Sub test2()
Range("A1").Value = Replace(Replace(Replace(Range("A1").Value, ":", "_"), "/", "_"), " ", "_")
End Sub
 
Upvote 0
Is there a way to do it while still using the array of invalid characters because I will eventually need to remove 10 or so invalid characters and report back to the user which invalid characters they entered.
 
Upvote 0
Is there a way to do it while still using the array of invalid characters because I will eventually need to remove 10 or so invalid characters and report back to the user which invalid characters they entered.
try this:

Code:
Sub test()
Dim i As Integer
Dim SearchString As String
 
SearchString = Range("A1").Value

FindChar = Array(":", "/", " ")

For Each c In FindChar
    For i = 1 To Len(SearchString)
        If Mid(SearchString, i, 1) = c Then
            Range("A1").Value = Replace(SearchString, c, "_")
            MsgBox Chr(34) & c & Chr(34) & " was Found at position " & i
            SearchString = Range("A1").Value
            Exit For
        End If
    Next i
Next c
End Sub
 
Upvote 0
Tweaked it just a bit. Works great thanks!!

Code:
Sub test()
Dim i As Integer
Dim Pos As Integer
Dim SearchString As String
Dim problems As String

SearchString = Range("A1").Value

FindChar = Array(" ", Chr(34), "/", "\", ":", "*", "?", "<", ">", "|")

For Each c In FindChar
    For i = 1 To Len(SearchString)
        If Mid(SearchString, i, 1) = c Then
            Pos = i
            Range("A1").Value = Replace(Range("A1").Value, c, "")
            problems = problems & vbNewLine & Chr(34) & c & Chr(34) & " was Found at position " & Pos
        End If
    Next i
Next c

If problems <> "" Then
    MsgBox "Invalid characters were found and removed:" & problems
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
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