sort and replace macro help

ace2356

New Member
Joined
May 17, 2017
Messages
10
I am trying to create a macro to sort and replace text and have an example below.

1) First I want to sort everything with column b from lowest to highest and that would sort the other columns with it, so each row keeps the same info
2) Next, replace the text in column D with a new word
- for example, "unlock cherry" will be "c"


ABCD
13Apple
22unlock cherry
31apple
44strawberry

<tbody>
</tbody>
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Assumes data begins in A1 and you want to replace any part of a col D cell that meets your criteria.
Code:
Sub Ace()
Const TextToReplace As String = "unlock cherry"
Const ReplcWith As String = "c"
Dim R As Range
Set R = Range("A1:D" & Cells(Rows.Count, "B").End(xlUp).Row)
Application.ScreenUpdating = False
With R
    .Sort key1:=[B1], order1:=xlAscending
    .Columns(4).Replace what:=TextToReplace, replacement:=ReplcWith, lookat:=xlPart
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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