【问题标题】:DYNAMIC EXCEL REPORT - Make Excel Formula automatically add itself when new rows added动态 EXCEL 报告 - 使 Excel 公式在添加新行时自动添加
【发布时间】:2021-10-20 00:46:15
【问题描述】:

我在 Sheet1 中有一个看起来像这样的表格

**Sport**
Basketball
Basketball
Basketball
Volleyball
Volleyball
Football
Football
Football
Football
Football
Football
Hockey
Hockey
Hockey

我在 Sheet2 中有一个表格,如下所示:

SPORT   Basketball  Volleyball  Football    Hockey
SCORE       3           2          6           3

我在 B1 中应用了以下公式: =TRANSPOSE(UNIQUE(过滤器(Sheet1!$A$2:$A$15,Sheet1!$A$2:$A$15"")))

B2 中的公式: =COUNTIF(Sheet1!$A$2:$B$15,Sheet2!B1)

但是当 Sheet1 中的列更新时。例如,将曲棍球场之一更改为高尔夫,这会在 HEADER 中更新,但下面的公式和格式不会在无需物理拖动的情况下进行。

SPORT   Basketball  Volleyball  Football    Hockey    Golf
SCORE       3           2          6           3

正如您所见,Gold 的分数是空的……我需要自动填充它。有没有办法让excel自动将列中连续的公式“拉”到添加的行中?

(简化数据和公式以便于理解!!)

【问题讨论】:

  • 您可以将 VBA 与更改事件一起使用
  • 我以前从未使用过 VBA,有没有办法在没有 VBA 的情况下做到这一点我尝试使用 Excel 表格,因为一旦添加新字段就会格式化所有内容......唯一的问题是我我正在使用公式来动态更新标题名称,而 excel 表不允许这样做:(
  • 这是我能想到的唯一方法。这并不难,如果你想要并且可以使用启用宏的文件,我可以告诉你如何操作。
  • 您要使用 Excel 表格还是仅使用常规范围的单元格?
  • 表格的解释有点模棱两可——可以使用一些关于内部数据性质的更多细节——但我想某种类型的 =ARRAYFORMULA 可以帮助你出来。

标签: excel excel-formula office365


【解决方案1】:

根据我对问题的理解,听起来您想要的效果可以使用=ARRAY_CONSTRAIN=ARRAYFORMULA 命令的组合来复制。 =ARRAYFORMULA 允许沿数组迭代公式或值——因此一个公式用值填充一个范围,而不是单个单元格。 =ARRAY_CONSTRAIN 可用于限制这个新数组的长度(例如,它不会添加一长排零)。

下面是我想出的一个例子。在这里,我们根据您的earlier question 将列转置为标题,因此当我在 A 列中添加值时,它们将被转置为第 4 行中的标题。使用沿 C 列长度向下的新公式,我们可以填充整行新数据:

=ARRAY_CONSTRAIN(ARRAYFORMULA(B5*$C$4:$N$4),1,COUNT(C4:N4))

这个公式可以拖到 C 列的长度,只要你喜欢。这个单一的公式填充了行的整个长度,并受到第 4 行长度的限制。(您需要的任何函数都可以替换ARRAYFORMULA 中的(B5*$C$4:$N$4)。)

您可以看到,当我添加新数字时,数据会自动更新。

注意:此解决方案适用于我错误地认为您正在使用的 Google 表格。但是,仔细检查后发现您使用的是 Microsoft excel,这可能需要一些不同的解决方案。我会尽快为这两个问题提供答案。

【讨论】:

【解决方案2】:

过程是在Worksheet对象后面添加代码

当您的工作表中发生两件事时,此代码将执行:

  1. When the selection is changed

  2. 当单元格或范围发生更改时

步骤:

在尝试之前备份您的文件

  1. 在 Excel 中打开工作簿按Alt + F11

  2. 双击包含表格的工作表

  1. 复制粘贴以下内容

代码:

Option Explicit

Dim targetTableColumnCount As Long
Const targetTableName As String = "TableName"

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    On Error GoTo CleanFail
    
    ' Set a reference to the table (Change TableName to the actual name of the table)
    Dim targetTable As ListObject
    Set targetTable = Range(targetTableName).ListObject
    
    ' Store table columns count
    targetTableColumnCount = targetTable.Range.Columns.Count
    
    ' Check if you're adding a column to the table
    If Not Intersect(Target, targetTable.Range) Is Nothing Then
        
        
    End If
    
    'MsgBox targetTableColumnCount
    
CleanExit:
    ' Reenable events
    Application.EnableEvents = True
    Exit Sub
    
CleanFail:
    MsgBox "Error: " & Err.Description
    GoTo CleanExit
    
End Sub



Private Sub Worksheet_Change(ByVal Target As Range)

    On Error GoTo CleanFail

    ' Disable events so nothing else is triggered by the changes you make
    Application.EnableEvents = False
    
    ' Set a reference to the table (Change TableName to the actual name of the table)
    Dim targetTable As ListObject
    Set targetTable = Range(targetTableName).ListObject
    
    ' Check if you're adding a column to the table
    If Not Intersect(Target, targetTable.Range) Is Nothing Then
        If targetTableColumnCount < targetTable.Range.Columns.Count Then
            ' Set a reference to the added column
            Dim newColumnRange As Range
            Set newColumnRange = Intersect(Target.EntireColumn, targetTable.DataBodyRange)
            
            ' Copy formulas from previous column
            newColumnRange.Formula = newColumnRange.Offset(0, -1).Formula
            
        End If
    End If
    
    ' Store table columns count
    targetTableColumnCount = targetTable.Range.Columns.Count

CleanExit:
    ' Reenable events
    Application.EnableEvents = True
    Exit Sub
    
CleanFail:
    MsgBox "Error: " & Err.Description
    GoTo CleanExit
End Sub

结果:

  1. 调整此行并将TableName 替换为您的Excel 表格的实际名称

行:

Const targetTableName As String = "TableName"

  1. 尝试添加列标题。上一列中的公式应复制到新列中。

让我知道它是否有效

【讨论】:

    【解决方案3】:

    通过在 Sheet1 中为您的运动范围创建一个表格,也可以不使用 VBA。 如果您的表名为Table1,请在Sheet2 B1 中使用以下内容 =TRANSPOSE(UNIQUE(Table1[**sport**]))

    Sheet2 B2 中使用: =COUNTIF(Table1[**sport**];FILTER($1:$1;($1:$1&lt;&gt;"")*($1:$1&lt;&gt;"SPORT")))

    如果您随后在 Sheet1 中添加一个值,Table1 将更新,Sheet2 中的数据也会更新。

    【讨论】:

      【解决方案4】:

      这是旧的,所以这可能不再是一个问题,但我在查找另一个问题时偶然发现。 这是你的公式:

      =COUNTIF(Sheet1!$A$2:$B$15,Sheet2!B1)
      

      我会改成:

      =COUNTIF(Sheet1!$A$2:$B$15,B1#)
      

      B1 上不需要 Sheet2 引用,# 指的是唯一数组公式的开头,并将公式动态应用于数组公式中的每个单元格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-06
        • 1970-01-01
        • 2015-08-04
        • 2017-12-23
        • 2021-02-18
        • 1970-01-01
        相关资源
        最近更新 更多