Add data from sheets dynamically

msbask

New Member
Joined
Feb 24, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
In my example, I have a workbook that tracks information about fruit orders.

I have manually set up the TOTAL page to pull the data from the individual fruit sheets, which works fine. However, if I add another fruit worksheet, I have to manually add another column to the TOTAL page.

There must be a way to have the total page automatically recognize that a new sheet has been added and just include that automatically (and remove the column if the tab is removed), but I'm struggling to figure it out.

Note that there would likely never be more than 25 fruits, if that makes a difference.

Thanks for any help!

Cell Formulas
RangeFormula
B1:B6B1=Apple!B1
C1:C6C1=Banana!B1
D1:D6D1=Orange!B1
 

Attachments

  • workbook.png
    workbook.png
    56.6 KB · Views: 13

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Start with only column A populated in the TOTAL sheet. You will not need any formulae. Then copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your TOTAL sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Click on the name of the last sheet in your workbook and then click the plus sign to add a new sheet. Double click the new sheet name to re-name it with the name of the fruit. Insert the fruit name in B1 and the additional data below it. When done, activate the TOTAL sheet and the new fruit data will be added automatically. If you want to delete a fruit, delete the desired fruit sheet. When you activate the TOTAL sheet, that fruit column will be deleted. Please note that the macro is executed each time the TOTAL sheet is activated.
VBA Code:
Private Sub Worksheet_Activate()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, wsName As Range, lRow As Long, srcRng As Range, rng As Range, lCol As Long, desWS As Worksheet
    Set desWS = Sheets("TOTAL")
    lCol = desWS.Cells(1, desWS.Columns.Count).End(xlToLeft).Column
    If lCol > 1 Then
        Set srcRng = desWS.Range("B1").Resize(, lCol - 1)
        For Each ws In Sheets
            If ws.Name <> "TOTAL" Then
                Set wsName = srcRng.Find(ws.Name, LookIn:=xlValues, lookat:=xlWhole)
                If wsName Is Nothing Then
                    lRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                    lCol = desWS.Cells(1, desWS.Columns.Count).End(xlToLeft).Column
                    desWS.Cells(1, lCol + 1).Resize(lRow).Value = ws.Range("B1").Resize(lRow).Value
                End If
            End If
        Next ws
    Else
       lRow = Sheets(2).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
       desWS.Cells(1, lCol + 1).Resize(lRow).Value = Sheets(2).Range("B1").Resize(lRow).Value
    End If
    lCol = desWS.Cells(1, desWS.Columns.Count).End(xlToLeft).Column
    Set srcRng = desWS.Range("B1").Resize(, lCol - 1)
    For Each rng In srcRng
        If Not Evaluate("isref('" & rng.Value & "'!A1)") Then
            rng.EntireColumn.Delete
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi,
In cell B1, you can have
Excel Formula:
=IFERROR(INDEX(MID(sheetnames,FIND("]",sheetnames)+1,255),COLUMNS($B$1:B1)),"")
provided you have created a Named Range sheetnames .... defined as follows =GET.WORKBOOK(1)&T(NOW())
 
Upvote 0
This is amazing and does exactly what I need it to do, except, if I change the number of apples, the TOTAL sheet doesn't update.
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Activate()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, wsName As Range, lRow As Long, srcRng As Range, rng As Range, lCol As Long, desWS As Worksheet
    Set desWS = Sheets("TOTAL")
    lCol = desWS.Cells(1, desWS.Columns.Count).End(xlToLeft).Column
    If lCol > 1 Then
        Set srcRng = desWS.Range("B1").Resize(, lCol - 1)
        For Each ws In Sheets
            If ws.Name <> "TOTAL" Then
                Set wsName = srcRng.Find(ws.Name, LookIn:=xlValues, lookat:=xlWhole)
                If wsName Is Nothing Then
                    lRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                    lCol = desWS.Cells(1, desWS.Columns.Count).End(xlToLeft).Column
                    desWS.Cells(1, lCol + 1).Resize(lRow).Value = ws.Range("B1").Resize(lRow).Value
                Else
                    lRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                    lCol = desWS.Cells(1, desWS.Columns.Count).End(xlToLeft).Column
                    desWS.Cells(2, lCol).Resize(lRow - 1).Value = ws.Range("B2").Resize(lRow - 1).Value
                End If
            End If
        Next ws
    Else
       lRow = Sheets(2).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
       desWS.Cells(1, lCol + 1).Resize(lRow).Value = Sheets(2).Range("B1").Resize(lRow).Value
    End If
    lCol = desWS.Cells(1, desWS.Columns.Count).End(xlToLeft).Column
    Set srcRng = desWS.Range("B1").Resize(, lCol - 1)
    For Each rng In srcRng
        If Not Evaluate("isref('" & rng.Value & "'!A1)") Then
            rng.EntireColumn.Delete
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top