Hola 
This is my first post but I've been to this site many times for useful information so thank you! In fact, the code below is based on some code I found here I believe.
My intention is to create x number of workbooks based on values in column k (RepAssign). X is precisely my hang up. I've been able to write code to filter by values in a column to copy the filtered data to a new workbook, however, the values were static in the past.
My new code is splitting out records evenly based on user input, a prompt asks the user how many ways to split the rows. For testing, I went with 3 so I'd like to end up with 3 new workbooks for example, leaving the original/source workbook as is. It could be anywhere from 2 to 20 workbooks being created, again depending on user input. See attached for sample data.
Existing code;
Sub test()
'Determine last column, add label
GetUsedColumns
ActiveSheet.Cells(1, UsedColumns + 1) = "RepAssign"
'Prompt user to choose how many splits are needed
Dim RepAssign As Long
RepAssign = InputBox(Prompt:="How many Rep Assignments?", _
Title:="Rep Assign", Default:="")
'Split records evenly and label assign 1, 2, 3 etc
Dim recCount As Long
Dim evenDiv As Long
Dim extraRecs As Long
Dim h As Long 'i
Dim j As Long 'j
GetUsedColumns
h = 1
Application.ScreenUpdating = False
With ActiveSheet
'Calculate splits
GetUsedRows
recCount = UsedRows - 1
extraRecs = recCount Mod RepAssign
evenDiv = (recCount - extraRecs) / RepAssign
Do While h < UsedRows
'Every team gets at least the same amount
For j = 1 To evenDiv
h = h + 1
.Cells(h, UsedColumns).Value = RepAssign
Next j
'Check if uneven amount, and if so, add a line
If j = evenDiv + 1 And extraRecs > 0 Then
h = h + 1
.Cells(h, UsedColumns).Value = RepAssign
extraRecs = extraRecs - 1
End If
'Next team queued up
RepAssign = RepAssign - 1
Loop
End With
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
Thanks!
Bookmarks