链接两个不同的 Excel 表和 auto-populate 一个填充另一个

Linking two different Excel tables and auto-populate one when populate another

我有两个不同的 excel 格式 table 一个靠近另一个。第一个 table(绿色 headers)是一个 table,我必须在 A 列中添加一些化学式,B 列将是我将从第二个 [= 添加的所有化合物的总和34=](黄色headers),代表元素周期系统!

我在 Table 2(黄色 headers)中用于计算化合物的公式是这样的:

=C*MAX(IFERROR(IF(FIND(C&ROW(:);MolM.[@[Mol. Formula]]);ROW(:);0);0);IFERROR(IF(FIND(C&CHAR(ROW(:));MolM.[@[Mol. Formula]]&"Z");1;0);0)) (CSE formula)

我通常更新新化合物的方式是,我在 A 列中手动添加新的化学式(没关系),然后将主公式拖到 Table 2(黄色 header) 计算所有元素,然后在 B 列中求和作为主要结果!

我的问题是,是否有可能更加自动化,当我在 A 列中键入新化合物时,它会像往常一样扩展 table,但也会扩展到 auto-expand 并计算其余部分化合物,没有它我手动拖动公式..?

希望这已经足够清楚了。

有没有可能做到这一点?唯一的解决方案是 Power Query 还是?

我在这里胡乱猜测。 假设您在 A3 中写入 NH3,然后让它在 I3 中打印 I2(“N”的值)和 C2 * 3(“H”的值) C3 中的 3 次,“H3”)。然后让 B3 使用 =SUM() 或类似方法计算总值。

你可以有一个 VBA sub 来查找值并打印它。 这是它的原型:

Sub molFunc(chem As String, formRow As Long)
Dim i As Long, c As String, atoms As Range, a as Range

Set atoms = Range("C1", Cells(1, Columns.count).End(xlToLeft))    
For i = 1 To Len(chem)

    If Not Mid(chem, i + 1, 1) = UCase(Mid(chem, i + 1, 1)) Then 
        c = Mid(chem, i, 2)
        i = i + 1
    Else
        c = Mid(chem, i, 1)
    End If
    
    For Each a In atoms
        If a.Value = c Then
            If IsNumeric(Mid(chem, i + 1, 1)) Then
                a.Offset(formRow - 1).Value = a.Offset(1) * Mid(chem, i + 1, 1)
            Else
                a.Offset(formRow - 1).Value = a.Offset(1)
            End If
        End If
    Next a
Next i
End Sub

然后您可以从您选择的工作表中的 Worksheet_Change 事件中调用它。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A:A")) Is Nothing And Target.count = 1 Then
        Application.EnableEvents = False
        Call molFunc(Target.Value, Target.Row)
        Application.EnableEvents = True
    End If
End Sub