DYNAMIC EXCEL REPORT - Make Excel Formula automatically add itself when new rows added

南楼画角 提交于 2021-02-11 12:13:03

问题


I have a table in Sheet1 that looks like this

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

I have a table in Sheet2 that looks like:

SPORT   Basketball  Volleyball  Football    Hockey
SCORE       3           2          6           3

I applied the following formula in B1: =TRANSPOSE(UNIQUE(FILTER(Sheet1!$A$2:$A$15,Sheet1!$A$2:$A$15<>"")))

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

However when the column in Sheet1 is updated. For example, changing one of the hockey fields to Golf, this is updated in the HEADER but the formula and formatting below is not carried across WITHOUT having to physically drag it across.

SPORT   Basketball  Volleyball  Football    Hockey    Golf
SCORE       3           2          6           3

As you can see the score for Gold is empty.... I need this to be filled automatically. Is there a way that I can have excel automatically "pull" the formula that is contiguous in the column into the added row?

(Simplified data and formula for ease of understanding!!)


回答1:


Based on my understanding of the question, it sounds like the effect you want can be replicated using a combination of the =ARRAY_CONSTRAIN and =ARRAYFORMULA commands. The =ARRAYFORMULA allows for formulas or values to be iterated along an array--so one formula fills in a range with values as opposed to a single cell. =ARRAY_CONSTRAIN can be used to restrict the length of this new array (so it doesn't add a long row of zeros, for example).

Below is an example I came up with. Here we have the columns being transposed as the header based on your earlier question so as I add values in column A they are being transposed as the header in row 4. Using a new formula down the length of column C, we can populate the entire row of new data:

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

This formula can be dragged down the length of column C however long you like. This single formula populates the entire length of the row and is limited by the length of row 4. (Whatever function you need can be replace the (B5*$C$4:$N$4) inside the ARRAYFORMULA.)

You can see that as I add new numbers the data updates automatically.

NOTE: This solution works in Google Sheets which I mistakenly believed you were using. However, it appears on closer inspection that you are using Microsoft excel which might require a bit of a different solution. I will try to provide answers to both soon.




回答2:


The process is to add the code behind the Worksheet object

This code will execute when two things happen in your Worksheet:

  1. When the selection is changed

  2. When a cell or range is changed

Steps:

BACKUP YOUR FILE BEFORE TRYING THIS

  1. In Excel with your workbook open Press Alt + F11

  2. Double click the sheet that has the table

  1. Copy-paste the following

Code:

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

Result:

  1. Adjust this line and replace TableName with the actual name of your Excel table

Line:

Const targetTableName As String = "TableName"

  1. Try adding a column header. The formulas from the previous column should be replicated in the new column.

Let me know if it works




回答3:


Without VBA is also possible by creating a Table for your sports range in Sheet1. If your Table is named Table1 use the following in Sheet2 B1 =TRANSPOSE(UNIQUE(Table1[**sport**]))

In Sheet2 B2 use: =COUNTIF(Table1[**sport**];FILTER($1:$1;($1:$1<>"")*($1:$1<>"SPORT")))

If you then add a value in Sheet1 Table1 will be updated and so will the data in Sheet2.



来源:https://stackoverflow.com/questions/65735768/dynamic-excel-report-make-excel-formula-automatically-add-itself-when-new-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!