0

The command Replace() does not work for me. I finally got it working but only by skipping over error messages. Why a command works with when there are errors, dont ask, here is my working code. Can anyone please explain where im going wrong?

If Target.Address(0, 0) = "E3" Then
   Range("E3").Select
   On Error Resume Next
   Selection.NumberFormat = "@"
   Selection.Replace What:="-", Replacement:=""
   Selection.Replace What:=" ", Replacement:=""
   Selection = UCase(Selection.Value)
End If

Also, why wouldn't something like this work?

selection.value = replace(selection.value," ", "")

A bit of context for those who want: I'm using this to remove " - " and spaces out of product style numbers automatically. eg 05402-pt072 004 needs to equal 05402pt072004.

Thanks to anyone who responds.

2
  • 2
    Try changing Selection to Target Commented Feb 12, 2018 at 16:23
  • 1
    If this is in a worksheet event such as Private Sub Worksheet_Change(ByVal Target As Range) remember to add Application.EnableEvents = False at the start and Application.EnableEvents = True at the end.... erm, as @ScottHoltzman had in his answer. Commented Feb 12, 2018 at 16:40

5 Answers 5

4

Given the existence of Target I assume the code is in a Worksheet_Change event.

That said, the following should work:

If Target.Address(0, 0) = "E3" Then

   Application.EnableEvents = False 'stop change event from firing again
   With Target
       .NumberFormat = "@"
       .Replace What:="-", Replacement:=""
       .Replace What:=" ", Replacement:=""
       .Value = UCase$(.Value)
   End With
   Application.EnableEvents = True

End If
Sign up to request clarification or add additional context in comments.

3 Comments

ohhhhhhhhhhhhhhhhhh so thats it. thank you sooooo much and you solved like half my sheets problems with the application.enableevent = false.
+1 for skipping the nefarious On Error Resume Next Yes, there is use for it but ONLY if closely followed by On Error Goto 0
Given that both .Find and .Replace 'remember' whatever parameters were used last by the user on the worksheet, a more complete definition of the optional parameters should be specified. Specifically in this user's case, LookAt:=xlPart.
1

I am assuming you are running into an infinite loop, as each time you update the targetcell, the worksheet_change event kicks in. How about

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim s As String
    If Target.Address = "$E$3" Then
        Application.EnableEvents = False
        s = Target
        s = Replace(s, " ", "")
        s = Replace(s, "-", "")
        Target = Format(s, "@")
        Application.EnableEvents = True
    End If
End Sub

Comments

0

On a new Excel worksheet write this:

Sub TestMe()

    Range("A1") = "05402-pt072 004"
    Stop
    Range("A1").Value = Replace(Range("A1").Value, " ", "")
    Stop
    Range("A1").Value = Replace(Range("A1").Value, "-", "")

End Sub
  • Then press F5. The 05402-pt072 004 appears on A1
  • Press F5. Now the text on A1 is 05402-pt072004.
  • Press F5. Now the text on A1 is 05402pt072004.

Enjoy!

As a next step, you may remove the .Value from the code. Then try to write With Range("A1") etc.

2 Comments

Why remove the .Value??
@tom - in this case it is a bit useless - stackoverflow.com/questions/32996772/…
0

This will also work:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$E$3" Then
       Target.NumberFormat = "@"
       Value = Target.Value
       Value = Replace(Value, "-", "")
       Value = Replace(Value, " ", "")
       Target.Value = UCase(Value)
    End If
End Sub

Comments

0

Try replacing your code with the following

If Not Application.Intersect(Target, Me.Range("E3")) Is Nothing Then
    With Target
        .NumberFormat = "@"
        Replace expression:=.Value2, Find:="-", Replace:=vbNullString
        Replace expression:=.Value2, Find:=" ", Replace:=vbNullString
        .Value2 = UCase(.Value2)
    End With
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.