Results 1 to 3 of 3

Thread: How to discover repeat text names in Range array?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2007
    Posts
    25

    How to discover repeat text names in Range array?

    Hello to all!
    I use Excel 2003 and I have an array of text data. This array consist of 15-20 text items in one column. I want to know has this array repeated text names or not. If I use Visual Basic with cycle in witch take’s the first name and then compare it with another’s, then take’s the second e.t.c. it ‘s very long. How can I do it faster? If I only want to know has this array repeated text names or not.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to discover repeat text names in Range array?

    it ‘s very long. How can I do it faster?
    If you sort the array and then do check, that will also be longer. The best way is to copy the array to excel cells and then sort them and then compare them. that is the fastest...

    For Example (Please amend it as per you requirements...)

    vb Code:
    1. Sub aaa()
    2.  
    3. 'Defining my array for test purpose
    4. Dim ar(21)
    5. For i = 1 To 20
    6.     ar(i) = "aaa" & i
    7.     'Creating a duplicate value
    8.     ar(21) = "aaa1"
    9. Next i
    10.  
    11.  
    12. 'Copy Data to Sheet
    13. For j = 1 To 21
    14.     Sheets("sheet1").Range("A" & j) = ar(j)
    15. Next j
    16.  
    17. 'Sort The data
    18. Range("A1").Select
    19. Range(Selection, Selection.End(xlDown)).Select
    20. Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
    21. OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    22. DataOption1:=xlSortNormal
    23.  
    24. 'Do the Comparing
    25. For k = 1 To 21
    26.     If Range("A" & (k + 1)).Value = Range("A" & k).Value Then
    27.         MsgBox "Match Found"
    28.     End If
    29. Next k
    30.  
    31. End Sub
    Last edited by Siddharth Rout; Apr 14th, 2008 at 10:28 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Junior Member Colin_L's Avatar
    Join Date
    Mar 2008
    Location
    London, UK
    Posts
    21

    Re: How to discover repeat text names in Range array?

    #2 is already a good suggestion. Here's my 2 cents....

    If your names are already in a range in the worksheet then you could just use the COUNTIF worksheet function + conditional formatting to highlight duplicate values.

    Since you specify that you only want to know if the array contains duplicate names and not that you want to know what those duplicate names are, a VBA alternative to #2 would be to do something like this (or a modified version thereof:
    Code:
    Sub Example()
        Dim varArrayToCheck As Variant
        Dim i As Integer, colTemp As Collection
        
        varArrayToCheck = Array("Jiura", "Colin", "Koolsid", "Simon", _
                                "Peter", "Ben", "Phillip", "Ian", "Jiura")
        
        Set colTemp = New Collection
        
        On Error Resume Next
        For i = LBound(varArrayToCheck) To UBound(varArrayToCheck)
           colTemp.Add varArrayToCheck(i), varArrayToCheck(i)
        Next i
        
        On Error GoTo 0
        If colTemp.Count <> UBound(varArrayToCheck) + 1 Then
            MsgBox "Array contains a duplicate name"
        Else
            MsgBox "Array does not contain a duplicate name"
        End If
        
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width