I have a table of data which I want to split into separate worksheets.
I have managed to split the table into separate sheets, however it does not keep its current format like column width and row height.
I prefer the table to be in same format as original sheet.
I also could not figure out how to separate my data if my table started from row 2, so I just added some code to delete row 1 first.
If I could keep the top row on all sheets, that would be nice.
Code im using:
Sample:
<tbody>
</tbody>
I have managed to split the table into separate sheets, however it does not keep its current format like column width and row height.
I prefer the table to be in same format as original sheet.
I also could not figure out how to separate my data if my table started from row 2, so I just added some code to delete row 1 first.
If I could keep the top row on all sheets, that would be nice.
Code im using:
Code:
Sub ColumnToSheets()
Sheets("Sheet1").Select
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Const sname As String = "Sheet1 (2)" 'change to whatever starting sheet
Const s As String = "D" 'change to whatever criterion column
Dim d As Object, a, cc&
Dim p&, i&, rws&, cls&
Set d = CreateObject("scripting.dictionary")
With Sheets(sname)
rws = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
cls = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
cc = .Columns(s).Column
End With
For Each sh In Worksheets
d(sh.Name) = 1
Next sh
Application.ScreenUpdating = False
With Sheets.Add(After:=Sheets(sname))
Sheets(sname).Cells(1).Resize(rws, cls).Copy .Cells(1)
.Cells(1).Resize(rws, cls).Sort .Cells(cc), 2, Header:=xlYes
a = .Cells(cc).Resize(rws + 1, 1)
p = 2
For i = 2 To rws + 1
If a(i, 1) <> a(p, 1) Then
If d(a(p, 1)) <> 1 Then
Sheets.Add.Name = a(p, 1)
.Cells(1).Resize(, cls).Copy Cells(1)
.Cells(p, 1).Resize(i - p, cls).Copy Cells(2, 1)
End If
p = i
End If
Next i
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End With
Sheets("sheet1").Activate
End Sub
Sample:
Project Summary | |||||
Prj Code | Prj Name | Branch | Task | Actual Cost | Cost Budget |
01200 | Project 1 | JHB | C | 18 872.28 | 50 318.18 |
01500 | Project 2 | CPT | C | 14 898.74 | 31 818.18 |
01520 | Project 2 | DBN | S | 109.00 | 1 090.90 |
07900 | Project 3 | PMB | C | 314 673.87 | 369 486.36 |
07900 | Project 3 | PMB | E | 26 400.00 | 26 400.00 |
07900 | Project 3 | PMB | P | 17 572.72 | 27 272.72 |
07900 | Project 3 | PMB | S | 85 857.45 | 245 454.54 |
08400 | Project 4 | DBN | T | 328.91 | 38 181.81 |
<tbody>
</tbody>