+ Reply to Thread
Results 1 to 2 of 2

ReDim Preserve Array if string does not exist

Hybrid View

  1. #1
    Registered User
    Join Date
    04-04-2015
    Location
    Vancouver, Canada
    MS-Off Ver
    Office 365
    Posts
    88

    ReDim Preserve Array if string does not exist

    Hi,
    I'm trying to add and preserve an 2d array. Empty to start.
    First column will contain string, and Second column will contain numbers of times occurred.

    I have a loop that goes through part of the sheet and takes a string from each cell let's call strStat.
    If strStat is not found in the first column of the array let's call arrStat then redim preserve and add to first column and 1 to second column.
    If strStat already exist in the first column of arrStat then add +1 to the second column.

    I really don't know where to start. Can I get some help?

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: ReDim Preserve Array if string does not exist

    Maybe you could use a dictionary instead of an array.
    If the string doesn't exists add it to the dictionary else add 1.

    Here's a quick example :

    Sub test()
        Dim ar
        Dim Dic, a, b
        Dim i As Long
    
        Set Dic = CreateObject("Scripting.dictionary")
        ar = Cells(1).CurrentRegion.Value
    
        For i = 1 To UBound(ar, 1)
            If Not Dic.exists(ar(i, 1)) Then
                Dic.Add ar(i, 1), 1
            Else
                Dic(ar(i, 1)) = Dic(ar(i, 1)) + 1
            End If
    
            a = Dic.keys
            b = Dic.items
        Next i
    
        With Cells(1, 3)
            .Resize(Dic.Count, 1) = Application.Transpose(a)
            .Offset(0, 1).Resize(Dic.Count, 1) = Application.Transpose(b)
        End With
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

+ 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] Redim preserve two dimensional array problem
    By wazimu13 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-08-2013, 05:29 PM
  2. [SOLVED] Dynamic Array Fails to Redim Preserve
    By rodalsa1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-07-2013, 09:52 AM
  3. Redim Preserve a multi-dimen. array.
    By david90 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-08-2012, 03:41 PM
  4. redim preserve multidimensional array
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-07-2011, 02:48 PM
  5. Redim Preserve 2D Array
    By kraljb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-12-2006, 03:23 PM

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