See attached file where I used this macro:
Private Sub cmd_Elaborate_Click()
Dim rs, sh As Worksheet, r As Long, lastRow As Long
Dim c As Integer, oldName As String, outRow As Long
Dim myPath As String, newName As String
Dim newWb As Workbook, newSh As Worksheet
Dim outFileName As String
Const adInteger = 3
Const adDate = 7
Const adVarChar = 200
myPath = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\"))
Set rs = CreateObject("ADODB.Recordset")
'create recordset fileds
With rs.fields
.Append "name", adVarChar, 100
.Append "items", adVarChar, 100
.Append "mySort", adInteger
End With
rs.Open
For Each sh In ThisWorkbook.Sheets
lastRow = sh.Cells(Rows.Count, "a").End(xlUp).Row
For r = 2 To lastRow
rs.addnew
rs("name") = sh.Cells(r, "a")
rs("items") = sh.Cells(r, "b")
rs("mySort") = CInt(Left(rs("items"), InStr(rs("items"), " ") - 1))
rs.Update
Next r
Next sh
'sort data for name and items
rs.Sort = "name, mySort"
'put data in new workbooks
Application.ScreenUpdating = False
Do While Not rs.EOF
If rs(0) <> oldName Then
If oldName <> "" Then
newWb.Close SaveChanges:=True
End If
newName = rs(0)
Set newWb = Workbooks.Add
Set newSh = newWb.ActiveSheet
outFileName = myPath & newName & ".xls"
If Dir(outFileName) <> "" Then
Kill outFileName
End If
newWb.SaveAs Filename:=myPath & newName, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
'labels on top
newSh.Cells(1, "a") = "Name"
newSh.Cells(1, "b") = "Items"
newSh.Range("1:1").Font.Bold = True
outRow = 1
oldName = newName
End If
outRow = outRow + 1
For c = 1 To 2
newSh.Cells(outRow, c) = rs(c - 1)
Next c
rs.moveNext
Loop
Application.ScreenUpdating = True
newWb.Close SaveChanges:=True
rs.Close
Set rs = Nothing
Set newWb = Nothing
Set newSh = Nothing
MsgBox ("Processing terminated")
End Sub
Regards,
Antonio
Bookmarks