+ Reply to Thread
Results 1 to 6 of 6

Problems assigning value to an array: Run-Time error 9

Hybrid View

  1. #1
    Registered User
    Join Date
    04-05-2010
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    6

    Problems assigning value to an array: Run-Time error 9

    Code is below:

    Trying to read the value of a cell to put only unique values into an array for later uses.

    Sub Unique()
    Dim ARComputer() As String, ARAssign() As Variant, I, intRow, intLastRow, x
    I = 0: r = 0
    
    intLastRow = Range("A65536").End(xlUp).Row
    
    ARComputer(I) = Cells(intLastRow, 1).Value ' Error occurs at this line Trying to assign first value and then use this later to compare to other values
    
    For intRow = intLastRow To 2 Step -1
        Rows(intRow).Select
        If ARComputer(I) <> Cells(intRow, 1).Value Then
            I = I + 1
            ReDim Preserve ARComputer(I)
            ARComputer(I) = Cells(intRow, 1).Value
        End If
    Next intRow
    
    End Sub

  2. #2
    Valued Forum Contributor Sadath31's Avatar
    Join Date
    03-02-2011
    Location
    Dammam, Saudi Arabia
    MS-Off Ver
    Office 365
    Posts
    452

    Re: Problems assigning value to an array: Run-Time error 9

    You can use this to remove duplicates
    Option Explicit
    
    Sub RemoveDuplicates()
        Dim AllCells As Range, Cell As Range
        Dim NoDupes As New Collection
        Dim i As Integer, j As Integer
        Dim Swap1, Swap2, Item
        
        Set AllCells = Range("A1:A105")
        
        On Error Resume Next
        For Each Cell In AllCells
            NoDupes.Add Cell.Value, CStr(Cell.Value)
    '       Note: the 2nd argument (key) for the Add method must be a string
        Next Cell
    
    '   Resume normal error handling
        On Error GoTo 0
    
    '   Sort the collection (optional)
        For i = 1 To NoDupes.Count - 1
            For j = i + 1 To NoDupes.Count
                If NoDupes(i) > NoDupes(j) Then
                    Swap1 = NoDupes(i)
                    Swap2 = NoDupes(j)
                    NoDupes.Add Swap1, before:=j
                    NoDupes.Add Swap2, before:=i
                    NoDupes.Remove i + 1
                    NoDupes.Remove j + 1
                End If
            Next j
        Next i
        
    '   Add the sorted, non-duplicated items to a ListBox
    x=1
        For Each Item In NoDupes
            Range(“A”&x)= Item
    	X=x+1
        Next Item
    
    End Sub

  3. #3
    Registered User
    Join Date
    04-05-2010
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Problems assigning value to an array: Run-Time error 9

    I'm a bit confused why adding to a collection does not include duplicates but adding to an array does. I could not locate anything in the syntax description that states a collection would not include duplicates.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Problems assigning value to an array: Run-Time error 9

    A collection does not allow entries with duplicate keys; it generates a run-time error, and does not add the item.

    The On Error Resume Next statement suppresses the error.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Problems assigning value to an array: Run-Time error 9

    Unique values in column A to array sq
    sub more_unique()
      columns(1).advancedfilter xlfiltercopy,,cells(1,26),true
      sq=columns(26).specialcells(2)
    End Sub



  6. #6
    Registered User
    Join Date
    04-05-2010
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Problems assigning value to an array: Run-Time error 9

    Thanks a bunch. The first part (collection) is what I needed for now. Now to just figure out the remaining part...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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