Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Private Type tCustomRow
  4.     strCNumber As String
  5.     strCName As String
  6.     strINumber As String
  7.     strIPrice As String
  8. End Type
  9. Sub whatever()
  10.  
  11.     Dim i As Long, a As Long
  12.     Dim lngTopRowsToStartFrom As Long
  13.     Dim bolAbortOnEmptyLine As Boolean
  14.     lngTopRowsToStartFrom = 2
  15.    
  16.     Dim objSheet As Worksheet
  17.     Set objSheet = ActiveWorkbook.Sheets(1)
  18.    
  19.     Dim allCustomers() As tCustomRow 'String
  20.    Dim bolInit As Boolean
  21.    
  22.     bolAbortOnEmptyLine = True
  23.    
  24.     For i = lngTopRowsToStartFrom To Rows.Count
  25.        
  26.         If Trim(Range("B" & i).Value) <> "" Then
  27.            
  28.             If bolInit Then
  29.                 ReDim Preserve allCustomers(1 To UBound(allCustomers) + 1)
  30.             Else
  31.                 ReDim allCustomers(1 To 1)
  32.                 bolInit = True
  33.             End If
  34.            
  35.             'allCustomers(UBound(allCustomers)) = Range("B" & i).Value
  36.            allCustomers(UBound(allCustomers)).strCNumber = Range("A" & i).Value
  37.             allCustomers(UBound(allCustomers)).strCName = Range("B" & i).Value
  38.             allCustomers(UBound(allCustomers)).strINumber = Range("C" & i).Value
  39.             allCustomers(UBound(allCustomers)).strIPrice = Range("D" & i).Value
  40.        
  41.         Else
  42.        
  43.             If bolAbortOnEmptyLine Then Exit For
  44.        
  45.         End If
  46.    
  47.     Next i
  48.    
  49.     bolInit = False
  50.    
  51.     Dim uniqueCustomers() As String
  52.     Dim bolAlreadyAdded As Boolean
  53.    
  54.     For i = 1 To UBound(allCustomers)
  55.    
  56.         If Not bolInit Then
  57.            
  58.             ReDim uniqueCustomers(1 To 1)
  59.             uniqueCustomers(1) = allCustomers(1).strCName
  60.             bolInit = True
  61.                
  62.         Else
  63.        
  64.             bolAlreadyAdded = False
  65.              
  66.             For a = 1 To UBound(uniqueCustomers)
  67.            
  68.                 If uniqueCustomers(a) = allCustomers(i).strCName Then
  69.                     bolAlreadyAdded = True
  70.                     Exit For
  71.                 End If
  72.            
  73.             Next a
  74.            
  75.             If Not bolAlreadyAdded Then
  76.            
  77.                 ReDim Preserve uniqueCustomers(1 To UBound(uniqueCustomers) + 1)
  78.                 uniqueCustomers(UBound(uniqueCustomers)) = allCustomers(i).strCName
  79.            
  80.             End If
  81.        
  82.         End If
  83.            
  84.     Next i
  85.    
  86.     Dim objSheetToInsertAfter As Worksheet
  87.     Dim objNewInsertedSheet As Worksheet
  88.     Set objSheetToInsertAfter = objSheet
  89.            
  90.     Dim strSafeSheetName As String
  91.    
  92.     Dim n As Long
  93.     For i = 1 To UBound(uniqueCustomers)
  94.        
  95.         Set objNewInsertedSheet = ActiveWorkbook.Sheets.Add(, objSheetToInsertAfter)
  96.        
  97.         objNewInsertedSheet.Cells(1, 1) = "Item Number"
  98.         objNewInsertedSheet.Cells(1, 2) = "Item Price"
  99.        
  100.         n = 2
  101.         For a = 1 To UBound(allCustomers)
  102.             If allCustomers(a).strCName = uniqueCustomers(i) Then
  103.                 objNewInsertedSheet.Cells(n, 1) = allCustomers(a).strINumber
  104.                 objNewInsertedSheet.Cells(n, 2) = allCustomers(a).strIPrice
  105.                 n = n + 1
  106.             End If
  107.         Next a
  108.        
  109.         strSafeSheetName = Replace(uniqueCustomers(i), "/", "")
  110.         strSafeSheetName = Replace(strSafeSheetName, "*", "")
  111.        
  112.         objNewInsertedSheet.Name = strSafeSheetName
  113.        
  114.         Set objSheetToInsertAfter = objNewInsertedSheet
  115.    
  116.     Next i
  117.    
  118.     objSheet.Activate
  119.  
  120. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement