Advertisement
Guest User

CopyNamesIntoNewWorksheet

a guest
Aug 6th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'function used to extract all unique customers from a list based on their name
  2. Function TransferUniqueCustomers()
  3.     'variables
  4.    Dim customers(1 To 500), name As String
  5.     Dim i, CustIndex, SearchIndex As Long
  6.     Dim wsData, wsCust As Worksheet
  7.    
  8.     'use the currently active workbook
  9.    With ActiveWorkbook
  10.    
  11.     'initialize variables
  12.    Set wsCust = Worksheet("Unique Customers")
  13.     Set wsData = Worksheet("Job List (Fiscal 2015-2016)")
  14.     CustIndex = SearchIndex = 1
  15.    
  16.     'for loop to collect all unique customer names
  17.    For i = 1 To i = 500
  18.         'create a variable for comparing the names and extract it from the correct column
  19.        name = wsData.Cells(i, 6).Text
  20.        
  21.         'check for name against array
  22.        For SearchIndex = 1 To SearchIndex = 500
  23.             'check if customer's name is equal to any prior entry
  24.            If name = customers(SearchIndex, 6) Then
  25.                 'equals another name in array so scrap search
  26.                SearchIndex = 501
  27.             ElseIf SearchIndex < 500 Then
  28.                 'continue searching
  29.            Else
  30.                 'customer not found in array so add customer into array
  31.                customers(CustIndex) = name
  32.                 CustIndex = CustIndex + 1
  33.             End If
  34.         Next
  35.     'step forward in the loop
  36.    Next
  37.    
  38.     'now that we have an accurate customer list enter data into column
  39.    For i = 1 To i = CustIndex
  40.         'load data into columns
  41.        wsCust.Cells(i, 1) = customers(i)
  42.     Next
  43.    
  44.     'update cell with correct number
  45.    wsCust.Cells(2, 1) = "Unique customers: " & CustIndex
  46.    
  47. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement