Try this code
Option Explicit
Sub split_data()
Dim lrow As Long, i As Long
Dim sname As String
Application.ScreenUpdating = False
With Worksheets("Sheet1")
.Sort.SortFields.Add Key:=Range("P:P") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange Range("A:W")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To lrow
If .Range("A" & i).Value <> "" Then
sname = .Range("P" & i).Value
If Not Evaluate("ISREF('" & sname & "'!A1)") Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sname
.Rows("1:1").Copy Worksheets(sname).Range("A1")
End If
.Range("A" & i & ":W" & i).Copy Worksheets(sname).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
Choose Insert | Module
Where the cursor is flashing, choose Edit | Paste
To run the Excel VBA code:
Choose View | Macros
Select a macro in the list, and click the Run button
Bookmarks