sheetspread
Well-known Member
- Joined
- Sep 19, 2005
- Messages
- 5,161
This is one of many similar macros that uses the advanced filter to give each category its own named tab:
Converting something like:
to:
and
for as many different groups as listed......
The column E formulas can only be seen in the alpha tab, because the references are identical to those in the original data sheet. Beta, gamma, etc only show values. Can this be fixed so all the formulas appear as such? Or is it a limit of using the filter instead of writing the code to copy, paste, and create each worksheet?
Code:
Option Explicit
Sub ParseItems()
'Author: Jerry Beaucaire
'Date: 11/11/2009
'Summary: Based on selected column, data is filtered to individual sheets
' Creates sheets and sorts sheets alphabetically in workbook
' 6/10/2010 - added check to abort if only one value in vCol
' 7/22/2010 - added ability to parse numeric values consistently
' 11/16/2011 - changed way Unique values are collected, no Adv Filter
Dim LR As Long, Itm As Long, MyCount As Long, vCol As Long, iCol As Long
Dim ws As Worksheet, MyArr As Variant, vTitles As String, TitleRow As Long
Application.ScreenUpdating = False
'Column to evaluate from, column A = 1, B = 2, etc.
vCol = 1
'Sheet with data in it
Set ws = Sheets("Data")
'Range where titles are across top of data, as string, data MUST
'have titles in this row, edit to suit your titles locale
vTitles = "A1:Z1"
TitleRow = Range(vTitles).Cells(1).Row
'Spot bottom row of data
LR = ws.Cells(ws.Rows.Count, vCol).End(xlUp).Row
'Get a temporary list of unique values from vCol
iCol = ws.Columns.Count
ws.Cells(1, iCol) = "key"
For Itm = 2 To LR
On Error Resume Next
If ws.Cells(Itm, vCol) <> "" And Application.WorksheetFunction _
.Match(ws.Cells(Itm, vCol), ws.Columns(iCol), 0) = 0 Then
ws.Cells(ws.Rows.Count, iCol).End(xlUp).Offset(1) = ws.Cells(Itm, vCol)
End If
Next Itm
'Sort the temporary list
ws.Columns(iCol).Sort Key1:=ws.Cells(2, iCol), Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
'Put list into an array for looping
MyArr = Application.WorksheetFunction.Transpose _
(ws.Columns(iCol).SpecialCells(xlCellTypeConstants))
'clear temporary list
ws.Columns(iCol).Clear
'Turn on the autofilter
ws.Range(vTitles).AutoFilter
'Loop through list one value at a time
'The array includes the title cell, so we start at the second value in the array
'In case values are numerical, we convert them to text with ""
For Itm = 2 To UBound(MyArr)
ws.Range(vTitles).AutoFilter Field:=vCol, Criteria1:=MyArr(Itm) & ""
If Not Evaluate("=ISREF('" & MyArr(Itm) & "'!A1)") Then 'create sheet if needed
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = MyArr(Itm) & ""
Else 'clear sheet if it exists
Sheets(MyArr(Itm) & "").Move After:=Sheets(Sheets.Count)
Sheets(MyArr(Itm) & "").Cells.Clear
End If
ws.Range("A" & TitleRow & ":A" & LR).EntireRow.Copy _
Sheets(MyArr(Itm) & "").Range("A1")
ws.Range(vTitles).AutoFilter Field:=vCol
MyCount = MyCount + Sheets(MyArr(Itm) & "").Range("A" & Rows.Count) _
.End(xlUp).Row - Range(vTitles).Rows.Count
Sheets(MyArr(Itm) & "").Columns.AutoFit
Next Itm
'Cleanup
ws.AutoFilterMode = False
ws.Activate
MsgBox "Rows with data: " & (LR - TitleRow) & vbLf & "Rows copied to other sheets: " _
& MyCount & vbLf & "Hope they match!!"
Application.ScreenUpdating = True
End Sub
Converting something like:
Excel Workbook | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Group | Name | Number | NextNumber | Formula | ||
2 | Alpha | Monday | 2 | 1 | 4 | ||
3 | Alpha | Tuesday | 5 | 1 | 10 | ||
4 | Alpha | Wednesday | 6 | 4 | 30 | ||
5 | Alpha | Thursday | 5 | 8 | 45 | ||
6 | Alpha | Friday | 1 | 8 | 9 | ||
7 | Alpha | Saturday | 3 | 0 | 3 | ||
8 | Alpha | Sunday | 4 | 9 | 40 | ||
9 | Alpha | Monday | 1 | 7 | 8 | ||
10 | Alpha | Tuesday | 1 | 2 | 3 | ||
11 | Alpha | Wednesday | 3 | 9 | 30 | ||
12 | Beta | Thursday | 4 | 9 | 40 | ||
13 | Beta | Friday | 6 | 9 | 60 | ||
14 | Beta | Saturday | 6 | 8 | 54 | ||
15 | Beta | Sunday | 9 | 0 | 9 | ||
16 | Beta | Monday | 8 | 7 | 64 | ||
17 | Beta | Tuesday | 2 | 5 | 12 | ||
18 | Gamma | Wednesday | 5 | 8 | 45 | ||
19 | Gamma | Thursday | 3 | 1 | 6 | ||
20 | Gamma | Friday | 2 | 4 | 10 | ||
21 | Gamma | Saturday | 3 | 3 | 12 | ||
22 | Gamma | Sunday | 3 | 6 | 21 | ||
23 | Gamma | Monday | 7 | 4 | 35 | ||
24 | Gamma | Tuesday | 9 | 2 | 27 | ||
25 | Gamma | Wednesday | 8 | 0 | 8 | ||
26 | Gamma | Thursday | 7 | 2 | 21 | ||
27 | Gamma | Friday | 4 | 5 | 24 | ||
28 | Gamma | Saturday | 7 | 5 | 42 | ||
Data |
to:
Excel Workbook | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Group | Name | Number | NextNumber | Formula | ||
2 | Alpha | Monday | 2 | 1 | 4 | ||
3 | Alpha | Tuesday | 5 | 1 | 10 | ||
4 | Alpha | Wednesday | 6 | 4 | 30 | ||
5 | Alpha | Thursday | 5 | 8 | 45 | ||
6 | Alpha | Friday | 1 | 8 | 9 | ||
7 | Alpha | Saturday | 3 | 0 | 3 | ||
8 | Alpha | Sunday | 4 | 9 | 40 | ||
9 | Alpha | Monday | 1 | 7 | 8 | ||
10 | Alpha | Tuesday | 1 | 2 | 3 | ||
11 | Alpha | Wednesday | 3 | 9 | 30 | ||
Alpha |
and
Excel Workbook | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Group | Name | Number | NextNumber | Formula | ||
2 | Beta | Thursday | 4 | 9 | 40 | ||
3 | Beta | Friday | 6 | 9 | 60 | ||
4 | Beta | Saturday | 6 | 8 | 54 | ||
5 | Beta | Sunday | 9 | 0 | 9 | ||
6 | Beta | Monday | 8 | 7 | 64 | ||
7 | Beta | Tuesday | 2 | 5 | 12 | ||
Beta |
for as many different groups as listed......
The column E formulas can only be seen in the alpha tab, because the references are identical to those in the original data sheet. Beta, gamma, etc only show values. Can this be fixed so all the formulas appear as such? Or is it a limit of using the filter instead of writing the code to copy, paste, and create each worksheet?