+ Reply to Thread
Results 1 to 6 of 6

Issue with System.Collections.ArrayList.Contains function

Hybrid View

  1. #1
    Registered User
    Join Date
    01-12-2021
    Location
    Canada
    MS-Off Ver
    365
    Posts
    9

    Issue with System.Collections.ArrayList.Contains function

    Hi

    I am writing a script that takes data from a source and puts it into a destination data table.

    The script should check if the record already exists in the destination table by checking the ID.

    I have used the ArrayList.Contain functionality to do this in the past with good success. I am running into a bug where the source data seems to always be added. When I check the data I see I have duplicate records. I thought my script was not allowing for that? I can't quite see what the issue is as I used very similar code previously and it worked fine.

    Here is some code. I can provide a sample file if that is helpful.

    Any thoughts would be appreciated.

    dim srcData as variant
    srcData = Range(srcRg).Value
    
    ' create a unique list of existing records
    
    Dim unqList As Object
    Set unqList = CreateObject("System.Collections.ArrayList")
    
    
    'add the existing destination data IDs as the unique list
    dim i as long
    i=1
    For i = 1 To tbl.DataBodyRange.Rows.Count
    
        unqList.Add tbl.DataBodyRange(i, 1)
          ' the first column of the table data body range is the ID column
    Next i
    
    
    i = 1
    For i = 1 To UBound(srcData, 1)
    
    
        If unqList.contains(srcData(i, 1)) = True Then
            
            'do nothing if the new record is already in the list.
            Debug.Print "This item already exists: " & srcData(i, 1)
    
    '--------->>>>>this is where I have the issue, it seems to add the record even if it already exists in the destination table<<<<<<-------
        
        Else
              ' add the row, this part works fine.
            Set newrow = Tbl.ListRows.Add
                For j = 1 To UBound(srcData, 2)
                    With newrow
                        .Range(j) = srcData(i, j)
                    End With
                Next j
    
        End If
    
    Next i

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Issue with System.Collections.ArrayList.Contains function

    Sample file would help. Are you certain it's not trailing spaces or something like that?

    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Registered User
    Join Date
    01-12-2021
    Location
    Canada
    MS-Off Ver
    365
    Posts
    9

    Re: Issue with System.Collections.ArrayList.Contains function

    Hi,

    Thanks for your reply and interest in helping me solve this issue.
    • I am not too sure about the trailing spaces suggestion, but when I do an EXACT() comparison of the two ID ranges it does return as TRUE...
    • I attached a sample sheet with 20 rows of dummy data. The dummy data is both in the source and the destination sheets, so I would expect that the script does not create any more rows in the destination table..
    • Note that I have also enabled conditional formatting of duplicates in the destination range in order to highlight where duplicates exist.


    Any and all help would be appreciated, as I will be managing a huge volume of this data in the next few months.

    Thank you!
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Issue with System.Collections.ArrayList.Contains function

    You should add the value to the ArrayList not the range.
    Change this:
        unqList.Add tbl.DataBodyRange(i, 1)
    to this:
        unqList.Add tbl.DataBodyRange(i, 1).Value

  5. #5
    Registered User
    Join Date
    01-12-2021
    Location
    Canada
    MS-Off Ver
    365
    Posts
    9

    Re: Issue with System.Collections.ArrayList.Contains function

    Thank you!

    That was indeed the issue. I will mark the thread as solved!

  6. #6
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Issue with System.Collections.ArrayList.Contains function

    You're welcome, glad to help & thanks for the feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Problems with excel 2016 and System.Collections.SortedList
    By colddeck84 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2018, 06:04 AM
  2. [SOLVED] is there any book to teach System.Collections.ArrayList, System.Collections.SortedList,
    By Flora20 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-01-2017, 06:35 AM
  3. [SOLVED] How to use CreateObject("System.Collections.SortedList") in VBA
    By vnzerem in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-20-2013, 10:44 PM
  4. Collections within class module collections
    By AndyLitch in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2013, 06:00 AM
  5. [SOLVED] File System Object Issue.
    By Max_Taylor in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-25-2012, 07:01 PM
  6. CreateObject("System.Collections.ArrayList")
    By pike in forum Tips and Tutorials
    Replies: 4
    Last Post: 07-09-2012, 11:49 AM
  7. Calculating a scoring system (issue #2)
    By jenparker1234 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-05-2006, 09:46 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1