Find the value of A1 anywhere in Column C and replace with the value of B1

jakepenner

New Member
Joined
Dec 11, 2019
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello! I'm just trying to find the value of my A1 cell within the range of Column C. And if that value is in Column C, I want to replace that value with the value in the adjacent cell B1.

Any help would be amazing. Thanks!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
VBA Code:
Option Explicit

Sub FindA()
    Dim i As Long, lr As Long
    lr = Range("C" & Rows.Count).End(xlUp).Row
    Dim crit As Variant
    crit = Range("A1")
    Application.ScreenUpdating = False
    For i = 1 To lr
        If Range("C" & i) = crit Then
            Range("C" & i) = Range("B1")
        End If
    Next i
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Here is another macro that should work for you...
VBA Code:
Sub FindAndReplace()
  [C:C].Replace [A1], [B1], xlWhole, , , False, False, False
End Sub
 
Upvote 0
Hi Rick, If the Within "Workbook" option is selected, your macro replaces in all the columns of the sheet and in all the sheets.


1576105986641.png


Find and Replace remembers the "Within" option, which there is no (optional) parameter for in Range.Find or Range.Replace.

If you want to absolutely guarantee that the replace is only on the Sheet in question, the trick then to reset the "Within" option to "Sheet" from "Workbook" is to do a Range.Find first, before attempting to Range.Replace.

Continuing with your example:

VBA Code:
Sub FindAndReplace()
  Dim ws As Worksheet, rng As Range
  Set ws = ActiveSheet
  Set rng = ws.Cells.Find("*")
  ws.Range("C:C").Replace [A1], [B1], xlWhole, , , False, False, False
End Sub
 
Upvote 0
Another way

VBA Code:
Sub FindReplace()
  With Range("C1", Range("C" & Rows.Count).End(xlUp))
    .Value = Evaluate("=IF({1},IF(" & .Address & "<>"""",IF(" & .Address & "=A1,B1," & .Address & "),""""))")
  End With
End Sub
 
Upvote 0
Hi Rick, If the Within "Workbook" option is selected, your macro replaces in all the columns of the sheet and in all the sheets.
I was not aware of this! Thanks for noting it for me.


Find and Replace remembers the "Within" option, which there is no (optional) parameter for in Range.Find or Range.Replace.
I hate the Find and Replace dialog boxes for two specific reasons. First, I hate the fact that the dialog box and the VB Find and VB Replace functions remember several setting from use-to-use no matter where they were set. The VB code should start from a fresh position each time and not remember things that were done in the dialog box and vice versa. The second thing I hate is that there is an "Options>>" button that toggles such a small amount of additional options... the dialog box with options displayed is so small that it should be always what is displayed and there should not be an option button at all.


Continuing with your example:

VBA Code:
Sub FindAndReplace()
  Dim ws As Worksheet, rng As Range
  Set ws = ActiveSheet
  Set rng = ws.Cells.Find("*")
  ws.Range("C:C").Replace [A1], [B1], xlWhole, , , False, False, False
End Sub
A quick test show that you do not have to vector through a Range variable... simply referencing ActiveSheet directly appears to work...
VBA Code:
Sub FindAndReplace()
  ActiveSheet.Cells.Find ("*")
  ActiveSheet.Columns("C").Replace [A1], [B1], xlWhole, , , False, False, False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,490
Messages
6,125,094
Members
449,205
Latest member
ralemanygarcia

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