Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VBA Find and replace keeps changing formatting of cell contents starting with "$"

the below code grabs the value from A2 which starts with "$" and replaces all values on the sheet with an input box.

The only issue is after find and replace it changes cell A2 to currency format because it starts with a $ and removes the $. Is there any way to avoid this?

Private Sub CommandButton1_Click()
Dim strInput As String

strInput = InputBox("Enter replacement value", Default:="$")
If strInput <> "" Then
Range("A2").Select
    Cells.Replace What:=Range("A2"), Replacement:=strInput, LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
        FormulaVersion:=xlReplaceFormula2
End If
End Sub

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try it with an apostrophe:

Private Sub CommandButton1_Click()
Dim strInput As String

strInput = InputBox("Enter replacement value", Default:="'$")
If strInput <> "" Then
    Cells.Replace What:=Range("A2"), Replacement:=strInput, LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
        FormulaVersion:=xlReplaceFormula2
End If
End Sub

Also, the Range("A2").Select is not required

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading