0

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!!)

6
  • You can use VBA with the change event Commented Jan 15, 2021 at 12:49
  • I've never used VBA before, is there a way to do this without VBA i tried using an excel table because that formats everything as soon as you add a new field... only problem is I am using a formula to dynamically update the header names and the excel table does not allow this :( Commented Jan 15, 2021 at 13:07
  • It's the only way I can think of. It's not difficult, if you want and can use a macro enabled file, I can show you how. Commented Jan 15, 2021 at 13:34
  • Are you going to work with an Excel table or just a regular range of cells? Commented Jan 15, 2021 at 14:21
  • The explanations of the tables are a bit ambiguous--could use some more details regarding the nature of the data inside--but I would guess that an =ARRAYFORMULA in some variety would be able to help you out. Commented Jan 15, 2021 at 14:49

4 Answers 4

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))

enter image description here

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.

enter image description here

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.

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

2 Comments

I have updated the question now sorry for the delay!
Its not letting me update on here so I created a new one: stackoverflow.com/questions/65739210/…
0

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

enter image description here

  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:

enter image description here

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

Line:

Const targetTableName As String = "TableName"

enter image description here

  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

Comments

0

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.

Comments

0

This is old, so this may no longer be an issue, but I stumbled across while looking up a different issue. This is your formula:

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

I would change it to:

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

You don't need the Sheet2 Reference on B1, and the # refers to the start of the unique array formula and applies the formula to each cell in the array formula dynamically.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.