Macro to find and replace

Thembela01

New Member
Joined
Jun 7, 2013
Messages
36
Office Version
  1. 2016
Platform
  1. Windows
Hi Gurus,

Im looking for a macro to and find replace whats in Column A with Column D. For instance It takes A2 and go to Column D to find a match, if it finds a match in Column D , it does nothing. If it doesnt find a match it replaces with the corresponding Payno on B2. If no match for UserID then replace with with Payno.
UserIdPaynoRoleValidated List
1003821710038217ANDRIES
ABELM1001721ABELM
ADOLPHINAS100241610038217
ADRIG00161725BEN
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try:
VBA Code:
Sub IfNotFoundReplaceWithB()
Dim columnsAB As Variant, columnD As Variant, MatchResult As Variant, i As Long
With ActiveSheet ' you may want to use it on inactive one
  columnsAB = .Cells(1, 1).Resize(.Cells(.Rows.Count, 1).End(xlUp).Row, 2).Value
  columnD = .Cells(2, 4).Resize(.Cells(.Rows.Count, 4).End(xlUp).Row - 1, 1).Value
  For i = 2 To UBound(columnsAB)
    MatchResult = Application.Match(columnsAB(i, 1), columnD, 0)
    If IsError(MatchResult) Then columnsAB(i, 1) = columnsAB(i, 2)
  Next i
  .Cells(1, 1).Resize(UBound(columnsAB), 2) = columnsAB
End With
End Sub


The result shall be:
UserId
10038217​
ABELM
1002416​
161725​
 
Upvote 0
Try this:
VBA Code:
Sub find_and_replace_v1()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate("=IF(COUNTIF(D:D," & .Address & ")," & .Address & "," & .Offset(, 1).Address & ")")
  End With
End Sub

In some versions of excel an IF is required at the beginning of the function evaluate, then try:

VBA Code:
Sub find_and_replace_v2()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate("=IF({1},IF(COUNTIF(D:D," & .Address & ")," & .Address & "," & .Offset(, 1).Address & "))")
  End With
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,668
Members
449,463
Latest member
Jojomen56

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