0

I have two tables in different worksheets like so:

Sample Data

What I need is a function that when inserting or deleting a row from Table 1 updates Table 2 across all rows but is only linked to column A. There is no formula in Excel that can be used to insert a row upon the mismatch of a cell's value?

The following formula goes through a range of values and evaluates if they match or don't.
=IF(NOT(EXACT(J11:J14,N11)),J11,N11)

I'm thinking if there is a way to insert a row, I can replace this with the false condition. If not, I will have to create a macro.

What may be a good way to do this?

Sample Data End

5
  • 1
    Is column A in both tables a unique ID? i.e. "1" will only appear in each table once? And what if someone deletes a row from Table1? Do you want the corresponding row to be deleted from Table2? Commented Apr 4, 2018 at 23:11
  • @jeffreyweir The values in column A are unique. Commented Apr 5, 2018 at 12:47
  • And what about the second question in my comment? Commented Apr 5, 2018 at 20:51
  • Ah okay. For future reference, better to point out in a comment that you have revised your question. Otherwise we wouldn't know anything has changed. Commented Apr 7, 2018 at 0:36
  • I certainly will. Commented Apr 7, 2018 at 17:30

1 Answer 1

1

First, make sure that your two Tables are indeed Excel Tables. (If not, select them one at a time and use the Ctrl + T keyboard shortcut to turn them into 'official' Excel Tables aka ListObjects)

Then select column A in Table1, and assign the named range "Primary" to it by writing "Primary" in the name box as shown below and then push Enter: enter image description here

Likewise give column A in Table2 the name "Secondary".

Note that these names are case sensitive, because we're going to be referencing them from VBA.

Put it in the Sheet Module corresponding to the worksheet that Table 1 is in.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim dic     As Object
Dim v1      As Variant
Dim v2      As Variant
Dim vItem   As Variant
Dim lo      As ListObject
Dim lr      As ListRow
Dim lc      As ListColumn

On Error GoTo errhandler
If Not Intersect(Range("Primary"), Target) Is Nothing Then
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    Set dic = CreateObject("Scripting.Dictionary")
    v1 = Range("Primary")
    v2 = Range("Secondary")
    Set lo = Range("Secondary").ListObject
    Set lc = lo.ListColumns(1)
    For Each vItem In v2
        If Not dic.exists(vItem) Then
            dic.Add vItem, vItem
        Else
            MsgBox "You have " & vItem & " in the table already!. Please rename the row and try again."
            GoTo errhandler
        End If
    Next vItem

    For Each vItem In v1
        If Not dic.exists(vItem) Then
            Set lr = lo.ListRows.Add
            Intersect(lr.Range, lc.Range).Value = vItem
        End If
    Next vItem
End If

errhandler:
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With

End Sub

From now on, anything new you add to the Primary column will be added to the Secondary column:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.