Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub whatever()
  2.  
  3.     Dim i As Long, a As Long
  4.     Dim lngTopRowsToStartFrom As Long
  5.     lngTopRowsToStartFrom = 2
  6.    
  7.     Dim objSheet As Worksheet
  8.     Set objSheet = ActiveWorkbook.Sheets(1)
  9.    
  10.     Dim allCustomers() As String
  11.     Dim bolInit As Boolean
  12.    
  13.     For i = lngTopRowsToStartFrom To Rows.Count
  14.        
  15.         If Trim(Range("B" & i).Value) <> "" Then
  16.            
  17.             If bolInit Then
  18.                 ReDim Preserve allCustomers(1 To UBound(allCustomers) + 1)
  19.             Else
  20.                 ReDim allCustomers(1 To 1)
  21.                 bolInit = True
  22.             End If
  23.            
  24.             allCustomers(UBound(allCustomers)) = Range("B" & i).Value
  25.        
  26.         End If
  27.    
  28.     Next i
  29.    
  30.     bolInit = False
  31.    
  32.     Dim uniqueCustomers() As String
  33.     Dim bolAlreadyAdded As Boolean
  34.    
  35.     For i = 1 To UBound(allCustomers)
  36.    
  37.         If Not bolInit Then
  38.            
  39.             ReDim uniqueCustomers(1 To 1)
  40.             uniqueCustomers(1) = allCustomers(1)
  41.             bolInit = True
  42.                
  43.         Else
  44.        
  45.             bolAlreadyAdded = False
  46.              
  47.             For a = 1 To UBound(uniqueCustomers)
  48.            
  49.                 If uniqueCustomers(a) = allCustomers(i) Then
  50.                     bolAlreadyAdded = True
  51.                     Exit For
  52.                 End If
  53.            
  54.             Next a
  55.            
  56.             If Not bolAlreadyAdded Then
  57.            
  58.                 ReDim Preserve uniqueCustomers(1 To UBound(uniqueCustomers) + 1)
  59.                 uniqueCustomers(UBound(uniqueCustomers)) = allCustomers(i)
  60.            
  61.             End If
  62.        
  63.         End If
  64.            
  65.     Next i
  66.    
  67.     Dim objSheetToInsertAfter As Worksheet
  68.     Dim objNewInsertedSheet As Worksheet
  69.     Set objSheetToInsertAfter = objSheet
  70.            
  71.     Dim strSafeSheetName As String
  72.     For i = 1 To UBound(uniqueCustomers)
  73.         Set objNewInsertedSheet = ActiveWorkbook.Sheets.Add(, objSheetToInsertAfter)
  74.        
  75.         strSafeSheetName = Replace(uniqueCustomers(i), "/", "")
  76.         strSafeSheetName = Replace(strSafeSheetName, "*", "")
  77.        
  78.         objNewInsertedSheet.Name = strSafeSheetName
  79.         Set objSheetToInsertAfter = objNewInsertedSheet
  80.         'MsgBox "inserting new sheet: " & vbCrLf & uniqueCustomers(i)
  81.    Next i
  82.    
  83.     objSheet.Activate
  84.  
  85. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement