How to split table in excel
-
- BronzeLounger
- Posts: 1285
- Joined: 01 May 2016, 09:58
How to split table in excel
Hello All,
If I have a table in excel, and want to split it based on ID with the header of the table as shown below, how to make this automatically and split information for each person in a separate table like below:
If I have a table in excel, and want to split it based on ID with the header of the table as shown below, how to make this automatically and split information for each person in a separate table like below:
You do not have the required permissions to view the files attached to this post.
-
- 5StarLounger
- Posts: 611
- Joined: 14 Nov 2012, 16:06
Re: How to split table in excel
Do not split tables.
Use a filter to filter information in a table.
Use a filter to filter information in a table.
-
- Administrator
- Posts: 79321
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: How to split table in excel
Here is a way to do it:
Code: Select all
Sub SplitTable()
Dim wss As Worksheet
Dim wst As Worksheet
Dim tbs As ListObject
Dim ids As Collection
Dim cel As Range
Dim rt As Long
Dim id As Variant
Application.ScreenUpdating = False
Set wss = ActiveSheet
Set tbs = wss.Range("A1").ListObject
Set ids = New Collection
On Error Resume Next
For Each cel In tbs.ListColumns("Employee ID").DataBodyRange
ids.Add Key:=CStr(cel.Value), Item:=cel.Value
Next cel
On Error GoTo 0
Set wst = Worksheets.Add(After:=wss)
wst.Range("Z1").Value = "Employee ID"
rt = 1
For Each id In ids
tbs.HeaderRowRange.Copy Destination:=wst.Range("A" & rt)
wst.Range("Z2").Value = id
tbs.Range.AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=wst.Range("Z1:Z2"), _
CopyToRange:=wst.Range("A" & rt + 1)
wst.Range("A" & rt).CurrentRegion.Rows(1).Delete Shift:=xlShiftUp
wst.ListObjects.Add Source:=wst.Range("A" & rt).CurrentRegion
rt = rt + wst.Range("A" & rt).CurrentRegion.Rows.Count + 1
Next id
wst.Range("Z1:Z2").Clear
wst.UsedRange.EntireColumn.AutoFit
Application.ScreenUpdating = True
End Sub
Last edited by HansV on 06 Feb 2023, 12:00, edited 1 time in total.
Reason: to correct the original code
Reason: to correct the original code
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1285
- Joined: 01 May 2016, 09:58
Re: How to split table in excel
Thank you very much Hans,
this is really amazing :)
Regards
this is really amazing :)
Regards
-
- PlatinumLounger
- Posts: 4961
- Joined: 31 Aug 2016, 09:02
Re: How to split table in excel
Thanks a lot, my tutor Hans for the perfect solution.
Another way
Another way
Code: Select all
Sub Test()
Const sResult As String = "Result", StartRow As Long = 3, RowsToInsert As Long = 2
Dim ky, vKey, ws As Worksheet, wsResult As Worksheet, tbl As ListObject, dict As Object, tblResult As Object, i As Long, j As Long
Application.ScreenUpdating = False
Set dict = CreateObject("Scripting.Dictionary")
Set ws = ThisWorkbook.Sheets("Sheet1")
Set tbl = ws.ListObjects(1)
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Worksheets(sResult).Delete
On Error GoTo 0
Application.DisplayAlerts = True
Set wsResult = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
With wsResult
.Name = sResult
For i = 1 To tbl.Range.Rows.Count
ky = tbl.DataBodyRange.Cells(i, 1).Value
If Not dict.Exists(ky) Then dict.Add ky, New Collection
dict(ky).Add tbl.DataBodyRange.Rows(i)
Next i
j = StartRow
For Each vKey In dict.Keys
If vKey <> Empty Then
tbl.HeaderRowRange.Copy Destination:=wsResult.Range("A" & j)
dict(vKey)(1).Resize(dict(vKey).Count, tbl.Range.Columns.Count).Copy Destination:=wsResult.Range("A" & j + 1)
Set tblResult = .ListObjects.Add(xlSrcRange, .Range("A" & j).Resize(dict(vKey).Count + 1, tbl.Range.Columns.Count), xlYes)
tblResult.Name = "Employee_" & vKey
.Range("A" & j + dict(vKey).Count + RowsToInsert).EntireRow.Insert
If j = StartRow Then tblResult.Range.EntireColumn.AutoFit
j = j + dict(vKey).Count + RowsToInsert + 1
End If
Next vKey
End With
Application.ScreenUpdating = True
End Sub
-
- StarLounger
- Posts: 58
- Joined: 10 Mar 2021, 22:57
Re: How to split table in excel
with Power Query
You do not have the required permissions to view the files attached to this post.
-
- 5StarLounger
- Posts: 611
- Joined: 14 Nov 2012, 16:06
Re: How to split table in excel
I'd use:
Code: Select all
Sub M_snb()
Sheet1.ListObjects(1).Range.Columns(1).AdvancedFilter 2, , Sheet1.Cells(1, 20), True
sn = Sheet1.Cells(1, 20).CurrentRegion
Sheet1.Cells(1, 20).CurrentRegion.Clear
With Sheet1.ListObjects(1).Range
For j = 2 To UBound(sn)
.AutoFilter 1, sn(j, 1)
.SpecialCells(12).Copy Sheet1.Cells(Rows.Count, 20).End(xlUp).Offset(2)
.AutoFilter
Next
End With
Sheet1.Cells(1, 20).Resize(2, Sheet1.ListObjects(1).Range.Columns.Count).Delete -4162
End Sub
-
- BronzeLounger
- Posts: 1285
- Joined: 01 May 2016, 09:58
Re: How to split table in excel
Dear Hamster,
Thank you very much for the support, could you please show me the steps how you did this please.
Regards
-
- BronzeLounger
- Posts: 1285
- Joined: 01 May 2016, 09:58
Re: How to split table in excel
Dear Snb Thank you very much for your useful codessnb wrote: ↑06 Feb 2023, 22:17I'd use:
Code: Select all
Sub M_snb() Sheet1.ListObjects(1).Range.Columns(1).AdvancedFilter 2, , Sheet1.Cells(1, 20), True sn = Sheet1.Cells(1, 20).CurrentRegion Sheet1.Cells(1, 20).CurrentRegion.Clear With Sheet1.ListObjects(1).Range For j = 2 To UBound(sn) .AutoFilter 1, sn(j, 1) .SpecialCells(12).Copy Sheet1.Cells(Rows.Count, 20).End(xlUp).Offset(2) .AutoFilter Next End With Sheet1.Cells(1, 20).Resize(2, Sheet1.ListObjects(1).Range.Columns.Count).Delete -4162 End Sub
Regards
-
- StarLounger
- Posts: 58
- Joined: 10 Mar 2021, 22:57
Re: How to split table in excel
steps are :
data tab - show queries - double click on Table1 and you'll see basic steps on the right side
to see M for that go Home - Advanced Editor
to make separate, result tables, right click on the each table on the list and choose Add As New Query
then load each Query to the sheet
You do not have the required permissions to view the files attached to this post.
-
- BronzeLounger
- Posts: 1285
- Joined: 01 May 2016, 09:58
Re: How to split table in excel
Thank you very much Hamster for the clarification, Now I know how to do it.
this is good for small data what if I have big data, I was not able to select multiple tables and then right-click and choose add as a new query, is there something we can do in this case?
the second question is: after creating queries and clicking Close&Load the tables went to separate sheets instead of one sheet, can we solve this also?
Regards
this is good for small data what if I have big data, I was not able to select multiple tables and then right-click and choose add as a new query, is there something we can do in this case?
the second question is: after creating queries and clicking Close&Load the tables went to separate sheets instead of one sheet, can we solve this also?
Regards
-
- StarLounger
- Posts: 58
- Joined: 10 Mar 2021, 22:57
Re: How to split table in excel
Power Query is just for BIG DATA
solution is tailored to your example
solution is tailored to your example
-
- BronzeLounger
- Posts: 1285
- Joined: 01 May 2016, 09:58
Re: How to split table in excel
in case we want to create 200 queries what will be the alternate solutions plz?
-
- StarLounger
- Posts: 58
- Joined: 10 Mar 2021, 22:57
Re: How to split table in excel
I am too lazy so check this one https://www.youtube.com/watch?v=Tnw77rpm-BQ
-
- BronzeLounger
- Posts: 1285
- Joined: 01 May 2016, 09:58
-
- StarLounger
- Posts: 58
- Joined: 10 Mar 2021, 22:57
Re: How to split table in excel
you are welcome but you won't be too happy with this video
-
- BronzeLounger
- Posts: 1285
- Joined: 01 May 2016, 09:58
Re: How to split table in excel
Dear Yasser, thank you very much for your code, I would like to add or enable the total row for each table and put this function in itYasserKhalil wrote: ↑06 Feb 2023, 13:43Thanks a lot, my tutor Hans for the perfect solution.
Another wayCode: Select all
Sub Test() Const sResult As String = "Result", StartRow As Long = 3, RowsToInsert As Long = 2 Dim ky, vKey, ws As Worksheet, wsResult As Worksheet, tbl As ListObject, dict As Object, tblResult As Object, i As Long, j As Long Application.ScreenUpdating = False Set dict = CreateObject("Scripting.Dictionary") Set ws = ThisWorkbook.Sheets("Sheet1") Set tbl = ws.ListObjects(1) Application.DisplayAlerts = False On Error Resume Next ThisWorkbook.Worksheets(sResult).Delete On Error GoTo 0 Application.DisplayAlerts = True Set wsResult = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) With wsResult .Name = sResult For i = 1 To tbl.Range.Rows.Count ky = tbl.DataBodyRange.Cells(i, 1).Value If Not dict.Exists(ky) Then dict.Add ky, New Collection dict(ky).Add tbl.DataBodyRange.Rows(i) Next i j = StartRow For Each vKey In dict.Keys If vKey <> Empty Then tbl.HeaderRowRange.Copy Destination:=wsResult.Range("A" & j) dict(vKey)(1).Resize(dict(vKey).Count, tbl.Range.Columns.Count).Copy Destination:=wsResult.Range("A" & j + 1) Set tblResult = .ListObjects.Add(xlSrcRange, .Range("A" & j).Resize(dict(vKey).Count + 1, tbl.Range.Columns.Count), xlYes) tblResult.Name = "Employee_" & vKey .Range("A" & j + dict(vKey).Count + RowsToInsert).EntireRow.Insert If j = StartRow Then tblResult.Range.EntireColumn.AutoFit j = j + dict(vKey).Count + RowsToInsert + 1 End If Next vKey End With Application.ScreenUpdating = True End Sub
=COUNTA([Clock-in/out Time])-COUNTBLANK([Clock-in/out Time])
-
- Administrator
- Posts: 79321
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: How to split table in excel
Your sample workbook does not have a column Clock-in/out Time...
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1285
- Joined: 01 May 2016, 09:58
Re: How to split table in excel
Dear Hans,
You are right, my apology for this, instead we can use Total Time column,
Regards
Siyamand
-
- Administrator
- Posts: 79321
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: How to split table in excel
COUNTA counts non-blank cells, so I don't think you need to subtract COUNTBLANK.
Below the line
insert these new lines:
Below the line
Code: Select all
tblResult.Name = "Employee_" & vKey
Code: Select all
tblResult.ShowTotals = True
tblResult.ListColumns("Total Time").TotalsCalculation = xlTotalsCalculationCount
Best wishes,
Hans
Hans