Advertisement
cheungtifan

Untitled

Apr 7th, 2012
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub createSheet()
  2.     Dim x As Integer
  3.     Dim uniqCnt As Integer
  4.     Dim UniqOrg(1024) As String
  5.     Dim People As New Scripting.Dictionary
  6.     Dim SheetMain_Name As String
  7.     Dim StartColumn As String
  8.     Dim EndColumn As String
  9.     Dim TableHeaderArea As String
  10.     Dim PeopleCollection
  11.     Dim TableLength As Integer
  12.     Dim CopyRegion As String
  13.     Dim TargetRegion As String
  14.     Dim StartTow As Integer
  15.     Dim CurrentRecordRow As Integer
  16.    
  17.     SheetMain_Name = "SheetMain"
  18.     StartRow = 7 ' Start row of data
  19.    StartColumn = "A"
  20.     EndColumn = "W"
  21.     TableHeaderArea = "A1:W6"
  22.     TableLength = 1
  23.     Application.Worksheets(1).Select
  24.     NumRows = Range("T7", Range("T7").End(xlDown)).Rows.Count
  25.     Range("T7").Select
  26.     uniqCnt = 0
  27.    
  28.     For x = 1 To NumRows
  29.        
  30.         If Len(ActiveCell.Text) > 0 Then
  31.             If InArray(UniqOrg, ActiveCell.Text) < 0 Then
  32.                 'ReDim Preserve UniqOrg(0 To (uniqCnt + 1)) As String
  33.                UniqOrg(uniqCnt) = ActiveCell.Value
  34.                
  35.                 Set MyPeopleCollection = New Collection
  36.                 MyPeopleCollection.Add ActiveCell.Row
  37.                 People.Add ActiveCell.Value, MyPeopleCollection
  38.                 uniqCnt = uniqCnt + 1
  39.             Else
  40.                 People.Item(ActiveCell.Value).Add (ActiveCell.Row)
  41.             End If
  42.            
  43.         End If
  44.         ActiveCell.Offset(1, 0).Select
  45.        
  46.     Next x
  47.    
  48.     ' Create worksheets according to UniqOrg()
  49.    For x = 0 To uniqCnt
  50.         'Debug.Print "value=" + UniqOrg(x)
  51.        If Len(UniqOrg(x)) > 0 Then
  52.             'Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = UniqOrg(x)
  53.            'Sheets(SheetMain_Name).Range(TableHeaderArea).Copy _
  54.             '    Destination:=Sheets(UniqOrg(x)).Range(TableHeaderArea)
  55.            TargetRow = StartRow
  56.             For cnt = 1 To People.Item(UniqOrg(x)).Count
  57.                 CurrentRecordRow = People.Item(UniqOrg(x)).Item(cnt)
  58.                 CopyRegion = StartColumn + Trim(Str(CurrentRecordRow)) + ":" + EndColumn + Trim(Str(CurrentRecordRow))
  59.                 TargetRegion = StartColumn + Trim(Str(TargetRow)) + ":" + EndColumn + Trim(Str(TargetRow))
  60.                 TargetRow = TargetRow + 1
  61.                 'Debug.Print CopyRegion, "->", TargetRegion
  62.                Sheets(SheetMain_Name).Range(CopyRegion).Copy _
  63.                     Destination:=Sheets(UniqOrg(x)).Range(TargetRegion)
  64.             Next cnt
  65.         End If
  66.     Next x
  67.    
  68.     ' Copy and Paste data into the worksheet.
  69.    ' Remember to keep the state
  70. End Sub
  71.  
  72. Function InArray(Arr, Search) As Long
  73.     InArray = -1
  74.     For x = LBound(Arr) To UBound(Arr)
  75.         If Search = Arr(x) Then
  76.             InArray = x
  77.             Exit Function
  78.         End If
  79.     Next x
  80. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement