+ Reply to Thread
Results 1 to 4 of 4

Thread: Array Help / Understanding

  1. #1
    Valued Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    245

    Array Help / Understanding

    Hello,

    Im hoping someone can help me.

    I have inherited the following code and although I understand exactly what it is doing, i am still very much in the learning stage when it comes to arrays.

    Option Explicit
    Option Base 1
    Public strValues() As String
    
    Sub AddUniqueValues()
    
        Dim strValues() As String
        Dim intNewValue As Integer
        Dim strCurrentValue As String
        Dim intTargetRow As Integer
        Dim blnValueExists As Boolean
        Dim i As Integer
        Dim j As Integer
        
        intNewValue = 1
        
        ReDim strValues(1)
        
        strValues(1) = Sheets(1).Range("E2").Value
        
        For i = 2 To Sheets(1).Range("A1").CurrentRegion.Rows.Count
        
            strCurrentValue = Sheets(1).Cells(i, 5).Value
            
            For j = 1 To UBound(strValues)
                   
                   blnValueExists = False
                
                If strCurrentValue = strValues(j) Then
                
                    blnValueExists = True
                    Exit For
                    
                 End If
        
            Next j
            
                If blnValueExists = False Then
                
                    intNewValue = intNewValue + 1
                
                    ReDim Preserve strValues(intNewValue)
                    
                    strValues(intNewValue) = strCurrentValue
        
                End If
        
        Next i
            
        intTargetRow = 1
            
        For i = 1 To UBound(strValues)
        
            Sheets(2).Cells(intTargetRow, 1) = strValues(i)
                
            intTargetRow = intTargetRow + 1
        Next i
        
    End Sub
    What im wanting to do is capture the row number of the unique value and add this into the array in order to then reference it back out to Sheets(2).Cells(intTargetRow, 2)

    Can anyone help?

    Any text to better help my understanding of how arrays can be manipulated and built up would be greatly appreciated.

    Thanks
    Attached Files Attached Files
    McCrimmon

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Array Help / Understanding

    Hello McC,

    Here is another way to find the uniques. This is faster and more flexible than your previous macro. The Dictionary object is an associative array, also known as a hash table in other languages. This allows you to randomly access array elements, check if they exist, add to them, and remove them. All the data is stored in pair values. A pair value consists of a "key" which is a unique element ID and an "item". The item can be any object or data type except for a User Defined Type.
    Sub AddUniqueValues1()
    
      Dim Cell As Range
      Dim Dict As Object
      Dim DstRng As Range
      Dim DstWks As Worksheet
      Dim Key As Variant
      Dim R As Long
      Dim Rng As Range
      Dim SrcWks As Worksheet
      
        Set SrcWks = Worksheets("sheet1")
        Set DstWks = Worksheets("Sheet2")
          
        Set DstRng = DstWks.Range("A2")
          
        Set Rng = SrcWks.Range("A1").CurrentRegion
        Set Rng = Intersect(Rng, Rng.Offset(1, 0))
          
          Set Dict = CreateObject("Scripting.Dictionary")
          'Uncomment the line below to ignore case for the Keys
          'Dict.CompareMode = vbTextCompare
          
          For Each Cell In Rng.Columns(5).Cells
            Key = Trim(Cell)
            If Key <> "" Then
              If Not Dict.Exists(Key) Then
                 Dict.Add Key, Cell
              End If
            End If
          Next Cell
          
          For Each Key In Dict.Keys
            DstRng.Offset(R, 0) = Dict(Key).Value
            R = R + 1
          Next Key
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    888

    Re: Array Help / Understanding

    Based on your original code, Try this:- Rows in column "B" sheet2.
    Option Explicit
    Option Base 1
    Public strValues() As String
    
    Sub AddUniqueValues()
    
        Dim strValues() As String
        Dim intNewValue As Integer
        Dim strCurrentValue As String
        Dim intTargetRow As Integer
        Dim blnValueExists As Boolean
        Dim i As Integer
        Dim j As Integer
        
        intNewValue = 1
        
        ReDim strValues(1 To 2, 1)
        
        strValues(1, 1) = Sheets(1).Range("E2").Value
            strValues(2, 1) = Sheets(1).Range("E2").Row
        
        For i = 2 To Sheets(1).Range("A1").CurrentRegion.Rows.Count
            strCurrentValue = Sheets(1).Cells(i, 5).Value
            For j = 1 To UBound(strValues, 2)
                    blnValueExists = False
                If strCurrentValue = strValues(1, j) Then
                    blnValueExists = True
                    Exit For
                End If
          Next j
            If blnValueExists = False Then
                   intNewValue = intNewValue + 1
                   ReDim Preserve strValues(1 To 2, intNewValue)
                   strValues(1, intNewValue) = strCurrentValue
                    strValues(2, intNewValue) = i
            End If
         Next i
            
        intTargetRow = 1
            
        For i = 1 To UBound(strValues, 2)
        
            Sheets(2).Cells(intTargetRow, 1) = strValues(1, i)
            Sheets(2).Cells(intTargetRow, 2) = strValues(2, i)
            intTargetRow = intTargetRow + 1
        Next i
        
    End Sub
    Regards Mick

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

    Re: Array Help / Understanding

    If I'm not mistaken a oneliner suffices:

    Sub snb()
      sheets(1).columns(5).advancedfilter xlfiltercopy ,,sheets(1).cells(1),true
    End Sub



+ 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.2.0