【问题标题】:Excel Linked Table to ColumnExcel 链接表到列
【发布时间】:2018-04-04 21:48:50
【问题描述】:

我在不同的工作表中有两个表格,如下所示:


我需要的是一个函数,当从 表 1 中插入或删除一行时,会在所有行中更新 表 2,但只链接到 A 列。没有Excel中的公式可用于在单元格值不匹配时插入一行?

以下公式遍历一系列值并评估它们是否匹配。
=IF(NOT(EXACT(J11:J14,N11)),J11,N11)

我在想如果有办法插入一行,我可以用 false 条件替换它。如果没有,我将不得不创建一个宏。

有什么好的方法可以做到这一点?

【问题讨论】:

  • 两个表中的A列是唯一ID吗?即“1”只会出现在每个表中一次?如果有人从 Table1 中删除了一行怎么办?是否要从 Table2 中删除相应的行?
  • @jeffreyweir A 列中的值是唯一的。
  • 那我评论中的第二个问题呢?
  • 啊,好吧。为了将来参考,最好在评论中指出您已经修改了您的问题。否则我们不会知道发生了什么变化。
  • 我一定会的。

标签: excel excel-formula linked-tables


【解决方案1】:

首先,确保您的两个表格确实是 Excel 表格。 (如果没有,一次选择一个,然后使用 Ctrl + T 键盘快捷键将它们变成“官方”Excel 表格,即 ListObjects)

然后选择 Table1 中的 A 列,并通过在名称框中写入“Primary”来为其分配命名范围“Primary”,如下图所示,然后按 Enter:

同样将表 2 中的 A 列命名为“次要”。

请注意,这些名称区分大小写,因为我们将从 VBA 中引用它们。

放入表1所在工作表对应的Sheet Module中。

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

从现在开始,您添加到主要列的任何新内容都将添加到次要列:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多