Replace text in column after filtered

onetap

New Member
Joined
Sep 25, 2017
Messages
13
Hello,

I'm working on a file and trying to write a code to replace the text in the first column once I apply a filter to the 2nd column. i.e., I filter column 2 for "Dogs" and need to replace column one (which has text of "cat") with "dogs". I have one row of headers which I would like to exclude. So far this is the code I have which isn't working

Selection.AutoFilter
ActiveSheet.Range("$A$1:$CO$4909").AutoFilter Field:=2, Criteria1:="Dog"

Range("A1").Select
ActiveCell.Offset(1, 0).Select
Cells(Columns("A").Rows.Count, "A").End(xlUp).Select
If ActiveCell.Value = "CAT" Then
ActiveCell.Value = "DOG"
ActiveCell.Offset(0, 1).Select
ActiveCell.Offset(0, -1).Select

End if

End Sub

Or even just a way to replace whatever text is in column A with "Dog", it doesn't have to say cat for it to be replace. In other words, I can select all of the active rows in the first column and have it say dog in each row
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try
Code:
Selection.AutoFilter
ActiveSheet.Range("$A$1:$CO$4909").AutoFilter Field:=2, Criteria1:="Dog"

Range("A2:A4909").SpecialCells(xlVisible).Replace "*", "Dog", , , , , False, False
 
Upvote 0
Try
Code:
Selection.AutoFilter
ActiveSheet.Range("$A$1:$CO$4909").AutoFilter Field:=2, Criteria1:="Dog"

Range("A2:A4909").SpecialCells(xlVisible).Replace "*", "Dog", , , , , False, False

Thank you

To add on to the first question, how would I replace the text in column A if the text in column B was dog:
I found a similar post that showed the below code, but it isn't working

If .Range("B" & r).Value = "Cat" Then
.Range("A" & r).Value = "Dog"
End If
 
Upvote 0
how would I replace the text in column A if the text in column B was dog:
That's what the code I supplied should do. If it's not, what is it doing?
 
Upvote 0
That's what the code I supplied should do. If it's not, what is it doing?

I was just curious how to do it without the filter. I have to do this with a lot of items so was thinking it might be easier not using a filter and just replacing the text if it matches a diff column
 
Upvote 0
Ok, this will replace the contents in col A, if col B is Dog
Code:
Sub ReplaceNoFilter()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace(Replace("if(@1=""Dog"",""Dog"",@)", "@1", .Offset(, 1).Address), "@", .Address))
   End With
End Sub
 
Upvote 0
Ok, this will replace the contents in col A, if col B is Dog
Code:
Sub ReplaceNoFilter()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace(Replace("if(@1=""Dog"",""Dog"",@)", "@1", .Offset(, 1).Address), "@", .Address))
   End With
End Sub

Really appreciate your help! going to give this a shot now.

Would you mind breaking this down logically for me if you don't mind?
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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