PDA

View Full Version : [SOLVED:] VBA find&replace with conditions



hotmochaccin
05-30-2022, 02:49 AM
Hello :)

I'm trying to use an IF statement to find and replace data from table.

Example:

City A | Account A | Account B | City B
New York | AAA | AAA | New York <- case 1 correct, skip
Newark | BBB | CCC | Denver < - case 2 correct, skip
Chicago | DDD | DDD | . <- case 3 incorrect, if Account A = Account B, then replace "." (City B) to City A


For now I have:


Dim Ary As Variant
Dim i As Long

Ary = Worksheets("X").ListObjects("Y").DataBodyRange.Value

For i = 1 To UBound(Ary)
ActiveWorkbook.Worksheets("X").Range("D:D").Replace ".", Ary(i, 1), xlWhole, xlWhole, False, False, False
Next i

But I don't know how to add the value condition (check if Accounts are equal)... Can someone suggest me how to do this? :)

arnelgp
05-30-2022, 04:15 AM
Dim Ary As Variant
Dim i As Long

Ary = Worksheets("X").ListObjects("Y").DataBodyRange.Value

For i = 1 To UBound(Ary)
'Debug.Print Ary(i, 2), Ary(i, 3)
If Ary(i, 2) = Ary(i, 3) Then
Ary(i, 4) = Ary(i, 1)
End If
Next i
Worksheets("X").ListObjects("Y").DataBodyRange = Ary

hotmochaccin
05-31-2022, 02:40 AM
Thank you arnelgp, works perfect! :)