The sheets can be named according to asset id but the sheets must consist of the same total number of assets.
Means i need to consolidate all the same number of assets into one sheet. (eg. 1 asset into 1 sheet, 100 assets into 1 sheet)
if there are more than 1 person who have 1 asset then should be consolidated into 1 sheet.
I have this macro but it can only hold 30 arguements.And i have to manually create 1000 sheets.
shDest = WorksheetFunction.Choose(AllCells.Rows.Count , "2 Assets", "3 Assets", "4 Assets", "5 Assets")
Sub abc()
Const shMain As String = "Main"
Dim shDest As String
Dim AllCells As Range, Cell As Range
Dim UniqueValues As New Collection
Dim i As Integer, j As Integer
Dim Swap1, Swap2, Item
Dim Lastrow As Long, DestLastRow As Long
Worksheets(shMain).AutoFilterMode = False
' Find lastrow on Column E
Lastrow = Worksheets(shMain).Cells(Rows.CountLarge, 1).End(xlUp).Row
' Get items are in Column E
Set AllCells = Worksheets(shMain).Range("E2:E" & Lastrow)
' The next statement ignores the error caused
' by attempting to add a duplicate key to the collection.
' The duplicate is not added - which is just what we want!
On Error Resume Next
For Each Cell In AllCells
UniqueValues.Add Cell.Value, CStr(Cell.Value)
' Note: the 2nd argument (key) for the Add method must be a string
Next Cell
' Resume normal error handling
On Error GoTo 0
' Sort the collection (optional)
For i = 1 To UniqueValues.Count - 1
For j = i + 1 To UniqueValues.Count
If UniqueValues(i) > UniqueValues(j) Then
Swap1 = UniqueValues(i)
Swap2 = UniqueValues(j)
UniqueValues.Add Swap1, before:=j
UniqueValues.Add Swap2, before:=i
UniqueValues.Remove i + 1
UniqueValues.Remove j + 1
End If
Next j
Next i
Application.ScreenUpdating = False
For Each Item In UniqueValues
With Worksheets(shMain)
.Range("$A$1:$V$" & Lastrow).AutoFilter Field:=4, Criteria1:=CStr(Item)
Set AllCells = .Range("$A$2:$V$" & Lastrow).SpecialCells(xlCellTypeVisible)
End With
shDest = WorksheetFunction.Choose(AllCells.Rows.Count , "2 Assets", "3 Assets", "4 Assets", "5 Assets")
With Worksheets(shDest)
AllCells.Copy .Range("a" & .Cells(Rows.CountLarge, "A").End(xlUp).Row + 1)
End With
Next
Worksheets(shMain).AutoFilterMode = False
Application.ScreenUpdating = True
Set AllCells = Nothing
Set Cell = Nothing
End Sub
Bookmarks