One question I come across a lot is when someone has a lot of data on a single worksheet, and wants to split it out to multiple worksheets based on the value of a column. For example, if someone had the following birthday data on a worksheet, and wanted to have a sheet for each month. Assume this sample data:
SHEET NAME: Birthdays
Name Month Day
Emmy August 27
Bill July 3
Andy November 24
Bob July 27
Dana June 24
And after running a macro:
SHEET NAME: August
Name Month Day
Emmy August 27
SHEET NAME: July
Name Month Day
Bill July 3
Bob July 27
SHEET NAME: November
Name Month Day
Andy November 24
SHEET NAME: June
Name Month Day
Dana June 24
That is a simplistic example, but I see something like that frequently. The following code will do that for you, just set the column to use for the splitting-data and run.
Option Explicit
Sub SplitIntoMultipleSheetsBasedOnColumn()
Dim TheColumn As Range, ValRG As Range
Dim UniqVals() As Variant, AllVals() As Variant
Dim FirstDataRow As Long, i As Long, Cnt As Long
'Unique values in the column specified by TheColumn are given their own worksheet,
' and their entire row is copied to that worksheet
Set TheColumn = Columns("G") 'must be a single column
FirstDataRow = 2 'so that the header row(s) aren't turned into a sheet
Set ValRG = Intersect(TheColumn, TheColumn.Worksheet.UsedRange, _
TheColumn.Worksheet.Rows(FirstDataRow & ":" & TheColumn.Worksheet.Rows.Count))
If ValRG Is Nothing Then
MsgBox "No data found. Exiting."
Exit Sub
End If
ReDim UniqVals(0)
Cnt = 0
AllVals = ValRG.Value
For i = 1 To UBound(AllVals, 1)
If Not InArray(UniqVals, AllVals(i, 1)) Then
ReDim Preserve UniqVals(Cnt)
UniqVals(Cnt) = AllVals(i, 1)
Cnt = Cnt + 1
End If
Next 'i
Application.ScreenUpdating = False
For i = LBound(UniqVals) To UBound(UniqVals)
Set ValRG = FoundRange(TheColumn, UniqVals(i))
With Sheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
.Name = ValidSheetName(UniqVals(i))
On Error GoTo 0
If FirstDataRow > 1 Then TheColumn.Worksheet.Range(TheColumn.Cells(1), _
TheColumn.Cells(FirstDataRow - 1)).EntireRow.Copy .Range("A1")
ValRG.EntireRow.Copy .Range("A" & FirstDataRow)
End With
Next 'i
Application.ScreenUpdating = True
End Sub
Private Function ValidSheetName(ByVal DesiredSheetName As String) As String
On Error Resume Next
ValidSheetName = Left(Replace(Replace(Replace(Replace(Replace(Replace(Replace( _
DesiredSheetName, ":", ""), "\", ""), "/", ""), "?", ""), "*", ""), "[", ""), _
"]", ""), 31)
End Function
Public Function InArray(ByRef vArray(), ByVal vValue) As Boolean
Dim i As Long
For i = LBound(vArray) To UBound(vArray)
If vArray(i) = vValue Then
InArray = True
Exit Function
End If
Next 'i
InArray = False
End Function
Function FoundRange(ByVal vRG As Range, ByVal vVal) As Range
Dim FND As Range, FND1 As Range
Set FND = vRG.Find(vVal, LookIn:=xlValues, LookAt:=xlWhole)
If Not FND Is Nothing Then
Set FoundRange = FND
Set FND1 = FND
Set FND = vRG.FindNext(FND)
Do Until FND.Address = FND1.Address
Set FoundRange = Union(FoundRange, FND)
Set FND = vRG.FindNext(FND)
Loop
End If
End Function
Note that you don't need to sort your sheet first, though it will make things run a bit smoother. I originally had the code go through an array containing the sheet data, but a lot of the format was being lost. This way takes slightly longer but everything stays to the new sheets. I use this for my job, and I'm sure other people can use it for theirs as well.
No comments:
Post a Comment