Replace one text with another

MadBern

New Member
Joined
May 10, 2019
Messages
37
Hey folks,

Is this possible?
I want a particular word to be replaced by a different word in the same cell as it's written in.

Example: If someone in cell A1 types "Apples" I want the text automatically change to "Fruits".

I've tried Conditional formatting and Data validation without success.
I get to respond correctly if I type in a number, f.ex. "1" and it changes to "Fruits", but not if I type in any kind of text.
 
OK for a range A1:F30 try the following code: (note i am using phone atm so can't actually test it! If it does not work and you don't have a solution already i can type one up at home when I get to my PC)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim iSect As Variant
Set rng = Range("A1:F30") 
Set iSect = Application.Intersect(rng, Target) 
    If iSect Is Nothing Then
        'do nothing
    Else
        If UCase(Target.Value) = "APPLES" Then
            Target.Value = "Fruits"
        End If
    End If
End


"Compile error: Expected end sub"
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Ahh shoot, almost wrote it all out on phone! I guess I take for granted the automatic "End Sub" added when creating procedures!

With that code, literally just add "End Sub" right at the bottom (After End) - or change "End" to "End Sub"
 
Upvote 0
OK for a range A1:F30 try the following code: (note i am using phone atm so can't actually test it! If it does not work and you don't have a solution already i can type one up at home when I get to my PC)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim iSect As Variant
Set rng = Range("A1:F30") 
Set iSect = Application.Intersect(rng, Target) 
    If iSect Is Nothing Then
        'do nothing
    Else
        If UCase(Target.Value) = "APPLES" Then
            Target.Value = "Fruits"
        End If
    End If
End

Worked like a charm! Thank you very much :)
 
Upvote 0
Worked like a charm! Thank you very much :)

You're welcome! I have just tested it here on PC and notice it throws errors from time to time when deleting multiple cells at once,
Try this version to eliminate the errors:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo EndProcedure
Dim rng As Range
Dim iSect As Variant
Set rng = Range("A1:F30")
Set iSect = Application.Intersect(rng, Target)
    If iSect Is Nothing Then
        'do nothing
    Else
        If UCase(Target.Value) = "APPLES" Then
            Target.Value = "Fruits"
        End If
    End If
End


EndProcedure:
End


End Sub
 
Upvote 0
You're welcome! I have just tested it here on PC and notice it throws errors from time to time when deleting multiple cells at once,
Try this version to eliminate the errors:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo EndProcedure
Dim rng As Range
Dim iSect As Variant
Set rng = Range("A1:F30")
Set iSect = Application.Intersect(rng, Target)
    If iSect Is Nothing Then
        'do nothing
    Else
        If UCase(Target.Value) = "APPLES" Then
            Target.Value = "Fruits"
        End If
    End If
End


EndProcedure:
End


End Sub

Yeah, I noticed that too, but I could've lived with it.
Anyway, thanks again! :)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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