Forum Discussion
Combine table name with each value in the first column
Hello,
I need a little help in Excel.
So I have some tables of the following structure scattered across multiple sheets. (Fake data)
Nokia | |
Phone model | Color |
6610i | Blue |
3310 | Yellow |
I want to gather all the data from every sheet into the following table
Manufacturer | Model | Color |
Nokia | 6610i | Blue |
Nokia | 3310 | Yellow |
OnePlus | 8 Pro | Ultramarine Blue |
The data table always start on column A.
Thanks,
Iulian
6 Replies
- Harun24HRBronze Contributor
Another way is to use REDUCE() to stack data automatically. In this solution you must enable Excel 4.0 Macros and save the workbook as .x;sm.
First make a named range SheetNames using name manager and following formula-
=TEXTAFTER(GET.WORKBOOK(1)&T(NOW()),"]")
Then call that named range as REDUCE() function array argument and stack data by in directing those sheets.
=DROP(REDUCE("",DROP(TOCOL(SheetNames),1),LAMBDA(a,x,VSTACK(a, IFNA(HSTACK(INDIRECT("'"&x&"'!A1"),FILTER(INDIRECT("'"&x&"'!A3:B500"),INDIRECT("'"&x&"'!A3:A500")<>"")),INDIRECT("'"&x&"'!A1"))))),1)
See the attached file.
I assumed that Nokia is the name of the table. For a more generic solution, you might run the following macro:
Sub CombineTables() Dim wsh As Worksheet Dim tbl As ListObject Dim rng As Range ReDim arr(1 To 3, 1 To 1) As String Dim r As Long Dim c As Long Dim m As Long Dim n As Long Application.ScreenUpdating = False n = 1 arr(1, n) = "Manufacturer" arr(2, n) = "Model" arr(3, n) = "Color" For Each wsh In Worksheets For Each tbl In wsh.ListObjects m = tbl.ListRows.Count ReDim Preserve arr(1 To 3, 1 To n + m) For r = 1 To m n = n + 1 For c = 1 To 3 arr(1, n) = tbl.Name arr(2, n) = tbl.DataBodyRange(r, 1) arr(3, n) = tbl.DataBodyRange(r, 2) Next c Next r Next tbl Next wsh Set wsh = Worksheets.Add(After:=Worksheets(Worksheets.Count)) Set rng = wsh.Range("A1").Resize(n, 3) rng.Value = Application.Transpose(arr) wsh.ListObjects.Add SourceType:=xlSrcRange, Source:=rng Application.ScreenUpdating = True End Sub
- Harun24HRBronze Contributor
One way could be using the following formula-
=VSTACK( IFNA(HSTACK(Sheet2!A1,Sheet2!A3:B4),Sheet2!A1), IFNA(HSTACK(Sheet3!A1,Sheet3!A3:B4),Sheet3!A1), IFNA(HSTACK(Sheet3!A1,Sheet4!A3:B4),Sheet4!A1) )
- iliulian
Microsoft
Could this be expanded to handle cases where there are multiple tables in some sheets or none in some sheets without activating any advanced features, just with basic functions?
- OliverScheurichGold Contributor
=LET(sheets,A1:A5, sheetscombine,DROP(REDUCE("",sheets,LAMBDA(x,y, VSTACK(x,IFERROR(TRIMRANGE(INDIRECT("'"&y&"'!A:B")),HSTACK("",""))))),1), removeheaders,FILTER(sheetscombine,NOT(BYROW(INDEX(sheetscombine,,1),LAMBDA(x,ISNUMBER(SEARCH("Phone",x)))))), filldownmanufacturer,SCAN("",MAP(INDEX(removeheaders,,1),INDEX(removeheaders,,2), LAMBDA(a,b,IF(REGEXTEST(b,"[A-Za-z]+"),"",a))),LAMBDA(a,b,IF(REGEXTEST(b,"[A-Za-z]+"),b,a))), stacked,HSTACK(filldownmanufacturer,removeheaders), VSTACK( HSTACK("Manufacturer","Model","Color"), FILTER(stacked,REGEXTEST(INDEX(stacked,,3),"[A-Za-z]+"))))
I understand you only want basic functions but just in case you want to log on to your Microsoft account and use all the latest functions in Excel online you can use this formula. Or perhaps other users want to try this formula which handles multiple tables in some sheets and no table in some sheets.