Finding and Replacing code

hitbid

Board Regular
Joined
Jan 21, 2016
Messages
114
The below code works well, and I do not necessarily need anything, but again, I've used the "Select" option in VBA, and was wondering how I could possibly remove the Select piece of this code. Like I said, it works fine and I have no problem with it, but if you have any suggestions on replacing Select I'd love to hear them. Thanks. (public dims were made for firstrow and lastrow, hence not shown here)

Code:
Sub findAndReplace()Dim newcol As String
Dim placecol As Double


' reset the variables to match the new formatted area.
firstrow = Cells(ActiveCell.Row, ActiveCell.Column).End(xlUp).Row
lastrow = Cells(firstrow, ActiveCell.Column).End(xlDown).Row
placecol = ActiveCell.Column
newcol = InputBox("Which column are we finding and replacing in?")


'select the column where the replacement will take place.
Columns(newcol & ":" & newcol).Select


'run the FOr loop through the new first row and last row. Will naturally start and end where the new rows are.
For icounter = firstrow To lastrow
    Selection.Replace What:=Cells(icounter, placecol).Value, Replacement:=Cells(icounter, placecol + 1).Value, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Next icounter
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Yep, just did that, should have tried a little longer before asking. This works great. Thanks.

Code:
'select the column where the replacement will take place.
'Removed this - Columns(newcol & ":" & newcol).Select


'run the FOr loop through the new first row and last row. Will naturally start and end where the new rows are.
For icounter = firstrow To lastrow
    Columns(newcol & ":" & newcol).Replace What:=Cells(icounter, placecol).Value, Replacement:=Cells(icounter, placecol + 1).Value, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Next icounter
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,744
Messages
6,174,254
Members
452,553
Latest member
red83

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