VBA - Split Table data into separate worksheets

Prish

Board Regular
Joined
Mar 30, 2016
Messages
91
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:
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>
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Is anyone able to assist?

There was an error in my code, revised as follows:

Code:
Const sname As String = "[COLOR=#FF0000]Sheet1[/COLOR]"  'change to whatever starting sheet
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,723
Messages
6,126,470
Members
449,315
Latest member
misterzim

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