Replace 2 values from 2 different tables at once

KnewIT

New Member
Joined
Apr 27, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,
It's my first post here.

I have 2 tables in Excel that come from VBA data extraction from some other software.
imgen1.jpg

I’d like for the common values in the NAME column on each table to be linked, meaning : if, for instance, I replaced ABC02 in the second table with ABC03 then the first table’s ABC02 would be replaced with ABC03 as well.
imgen2.jpg

and conversely (a change in the first table would lead to a change in the second one).

I've tried using the Handle value to mark identical values with the following code:

VBA Code:
Sub Test1()
Dim i, y As Integer

For i = 10 To 11
    y = 7
    Do Until y = 5
        y = y - 1
        If Range("C" & y).Value = Range("C" & i).Value Then
            Range("D" & y).Value = Range("B" & i).Value
        End If
    Loop

'' Action :

Next i

End Sub

Which gave this result:
Test.xlsm
ABCD
3File1.dwg
4BlocknameHandleNAME
5Block1386ABC0227F
6Block22F7ABC01291
7Block3267
8File2.dwg
9BlocknameHandleNAME
10Block1291ABC01
11Block227FABC02
TEST

But I don't know how to efficiently do it both ways (meaning getting the handles of similar values for the other table as well) to get that result:
Test.xlsm
ABCD
3File1.dwg
4BlocknameHandleNAME
5Block1386ABC0227F
6Block22F7ABC01291
7Block3267
8File2.dwg
9BlocknameHandleNAME
10Block1291ABC012F7
11Block227FABC02386
Feuil6

and how to go from there (or even if I should do that at all ?)

I tried to be as clear as possible by including visuals but if there's anything I can clearify further, don't hesitate to tell me.

Thanks for reading and for any input.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Welcome to the Board!

Why not just use Find/Replace?
For example, if you want to change "ABC02" to "ABC03", simply find and replace all instances of "ABC02" with "ABC03" (Excel's "Replace All" feature will do them all for you at once).

If you really want VBA, you could do this via VBA, where you prompt the user to enter the original value, then prompt them for what they want to change it to, and then have VBA do the Find/Replace All.
 
Upvote 0
Welcome to the Board!

Why not just use Find/Replace?
For example, if you want to change "ABC02" to "ABC03", simply find and replace all instances of "ABC02" with "ABC03" (Excel's "Replace All" feature will do them all for you at once).

If you really want VBA, you could do this via VBA, where you prompt the user to enter the original value, then prompt them for what they want to change it to, and then have VBA do the Find/Replace All.
Thanks!

Actually, I have to use VBA and can't prompt the user for input.
Someone from another forum posted the following answer (so I'm posting it here in case anyone is looking for the same thing):

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Variant
Application.EnableEvents = False
Set f2 = Sheets("Image")
If Not Intersect(Target, Columns(3)) Is Nothing Then
    If Range(Target.Address).Value <> f2.Range(Target.Address).Value Then
        TargetImage = f2.Range(Target.Address).Value
        With f2
            Set x = Columns(3).Find(TargetImage)
            If Not x Is Nothing Then
                Pos = x.Address
                Do
                    If x.Row <> Target.Row Then
                        Cells(x.Row, "C") = Target.Value
                        'Modification of the Image sheet
                        f2.Cells(x.Row, "C") = Target.Value
                        f2.Range(Target.Address).Value = Target.Value
                    Else
                        Set x = .FindNext(x)
                    End If
                Loop While Not x Is Nothing And x.Address <> Pos
            End If
        End With
    End If
End If
Set f2 = Nothing
Application.EnableEvents = True
End Sub

This code assumes that:
  1. There are 2 identical sheets and the second one is called "Image"
  2. The user input is in the first sheet
  3. The values we want to replace are both in the 3rd column
  4. There are no duplicate values within the same table
If you want to execute another macro beforehand (like I do with my data extraction) you have to use that bit of code within that macro

VBA Code:
    On Error GoTo ErrHandler
    Application.EnableEvents = False

'Your code here...

ErrHandler:
   Application.EnableEvents = True

If somehow you modify that Worksheet_Change macro and get an error, you have to fix that error and enable events again with another macro like this one:

VBA Code:
Sub ReenableEventsAfterError()
Application.EnableEvents = True
End Sub
 
Upvote 0
Someone from another forum posted the following answer (so I'm posting it here in case anyone is looking for the same thing):
Thank you for posting back.

If posting on other forums (known as "Cross-Posting"), please note our policy on that. While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Most other forums also have the same policy (or do not allow it altogether).
 
Upvote 0
Thank you for posting back.

If posting on other forums (known as "Cross-Posting"), please note our policy on that. While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

Most other forums also have the same policy (or do not allow it altogether).

OK, thanks and sorry. ?

I will include the link to the other forum but I'm still unaware of their rules on cross-posting (I haven't seen anything about it in their rules).
That forum is French and insists on using French only.
 
Upvote 0
I will include the link to the other forum but I'm still unaware of their rules on cross-posting (I haven't seen anything about it in their rules).
That forum is French and insists on using French only.
OK. I was thinking most of the other big international Excel help forums. Most have similar policies.
Basically, it is important to know that the question has been asked elsewhere, so people do not waste time working up solutions that may have already been suggested elsewhere, or worse yet, they may waste time working on something that has already been solved (no one likes to feel that their time and efforts are wasted).
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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