Hello Rickard,
I'm not totally sure if I have understood your request properly but the following VBA code, assigned to a button, may help:-
Sub CreateNewShtsTransferData()
Dim sht As Worksheet
Dim lr As Long, i As Long
Dim GID As Object
Dim key As Variant
Set sht = Sheets("Enrolment")
Set GID = CreateObject("Scripting.Dictionary")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
lr = sht.Range("A" & Rows.Count).End(xlUp).Row
sht.Range("A2", sht.Range("F" & sht.Rows.Count).End(xlUp)).Sort sht.[A2], 1
For i = 2 To lr
If Not GID.Exists(sht.Range("A" & i).Value) Then
GID.Add sht.Range("A" & i).Value, 1
End If
Next i
For Each key In GID.keys
If Not Evaluate("ISREF('" & key & "'!A1)") Then
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = key
End If
If key = 0 Or key = vbNullString Then
MsgBox "Any zero group values will not be accounted for. No sheet will created and no data will be transferred.", vbExclamation, "WARNING"
Else
Sheets(key).Cells.Clear
sht.Range("A1:A" & lr).AutoFilter 1, key
sht.[A1].CurrentRegion.Copy Sheets(key).[A1]
Sheets(key).Columns.AutoFit
sht.[A1].AutoFilter
End If
Next key
On Error Resume Next
Sheets("0").Delete
sht.Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "All done!", vbExclamation
End Sub
This code will actually create the new Group sheets for you based on the Group names that you can select from a drop down box in each cell in Column A in the "Enrolment" sheet. It will then transfer the relevant rows of data to their respective sheets. Should you select "0" from any of the drop downs, a message box will appear advising that row values will not be accounted for and no new sheet will be created or data transferred. On clicking a button, all sheets will be refreshed to account for any changes.
The data is sorted based on Column A prior to any data transfer taking place.
I've attached a sample workbook so that you can see how this would work. Click on the "RUN" button to see it work.
I hope that this helps.
Cheerio,
vcoolio.
Bookmarks