+ Reply to Thread
Results 1 to 11 of 11

Split VBA array according to specific values

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Split VBA array according to specific values

    Hello everyone
    I have a range which is stored in a variable (arr) then it is stored in an array .. Range A2:Dx
    In the third column of this array there are multiple values ..
    I need to split the variable arr according to specific values .. The values are "A" & "B" & "D" & "Other"

    So as for "A" (the output new created array) >> will hold the values of "A" in column C
    As for "B" (the output new created array) >> will hold the values of "B" in column C
    As for "D" (the output new created array) >> will hold the values of "D" in column C
    As for any other entires, the output new created array will be "Other"

    I imagine to put these values as needed in an array

    x=Array("A","B","D","Other")

    Finally I need as in the example four arrays for each value

    Hope it is clear
    Thanks advanced for help
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Split VBA array according to specific values

    So what exactly are you trying to do and what should the end result look like? Can you manually create expected output?
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Split VBA array according to specific values

    The output will be arrays .. I mean sub-arrays from this main array (the same idea as filter) in ranges .. but with arrays in that case
    I imagine to get for example : arr_A and this will be an array that holds the values of the rows that has the value "A" in column C ...

    I mean to produce arrays through the code itself ... Is it possible ?

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Split VBA array according to specific values

    Yes. Something like below. Though I'd probably use some other method, if I had to do something like this.

    Sub Demo()
    Dim tempArr, arr_A, arr_B, arr_D, arr_Oth
    Dim a As Long, b As Long, d As Long, o As Long: a = 1: b = 1: d = 1: o = 1
    
    tempArr = Cells(1).CurrentRegion
    
    With Application
        ReDim arr_A(1 To .CountIf(Sheet1.Cells(1).CurrentRegion.Columns(3), "A"), 1 To 4)
        ReDim arr_B(1 To .CountIf(Sheet1.Cells(1).CurrentRegion.Columns(3), "B"), 1 To 4)
        ReDim arr_D(1 To .CountIf(Sheet1.Cells(1).CurrentRegion.Columns(3), "D"), 1 To 4)
        ReDim arr_Oth(1 To UBound(tempArr) - UBound(arr_A) - UBound(arr_B) - UBound(arr_D), 1 To 4)
    End With
    
    For i = 2 To UBound(tempArr)
        Select Case tempArr(i, 3)
        Case "A"
            arr_A(a, 1) = tempArr(i, 1)
            arr_A(a, 2) = tempArr(i, 2)
            arr_A(a, 3) = "A"
            arr_A(a, 4) = tempArr(i, 4)
            a = a + 1
        Case "B"
            arr_B(b, 1) = tempArr(i, 1)
            arr_B(b, 2) = tempArr(i, 2)
            arr_B(b, 3) = "B"
            arr_B(b, 4) = tempArr(i, 4)
            b = b + 1
        Case "D"
            arr_D(d, 1) = tempArr(i, 1)
            arr_D(d, 2) = tempArr(i, 2)
            arr_D(d, 3) = "D"
            arr_D(d, 4) = tempArr(i, 4)
            d = d + 1
        Case Else
            arr_Oth(d, 1) = tempArr(i, 1)
            arr_Oth(d, 2) = tempArr(i, 2)
            arr_Oth(d, 3) = "Other"
            arr_Oth(d, 4) = tempArr(i, 4)
            o = o + 1
        End Select
    Next
    
    Range("K1").Resize(UBound(arr_A), 4) = arr_A
    
    
    End Sub

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Split VBA array according to specific values

    Thanks a lot Mr. Chihro for this great code
    I think this part
        Case Else
            arr_Oth(d, 1) = tempArr(i, 1)
            arr_Oth(d, 2) = tempArr(i, 2)
            arr_Oth(d, 3) = "Other"
            arr_Oth(d, 4) = tempArr(i, 4)
            o = o + 1
    d would be replaced with o

    Generally this is fine and I can simulate that but I seek for generic approach to create the sub-arrays automatically .. through looping the elements of the array ("A","B","D","Other") without declaring variables .. I need the code itself to create these variables for me
    I know it may be weird but I hope to find a way

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Split VBA array according to specific values

    For that you'd need to use dictionary or some other collection to hold individual arrays.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Split VBA array according to specific values

    Quote Originally Posted by CK76 View Post
    For that you'd need to use dictionary or some other collection to hold individual arrays.
    So it is possible to do that by using collections.. That's great
    But I have a little experience in using collections and I can't imagine the process well. Can you help me in that please?

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Split VBA array according to specific values

    Can you clarify few things before I think on logic for sample code?

    "A", "B", "D", "Other", these category are constant or declared at start correct?

    Also, you don't want multi-level dictionary/collection objects, but array as end result?

    FYI - In my opinion, much easier to just build multi-level dictionary as it gives more control and far more flexible than array if you want to retrieve info.

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Split VBA array according to specific values

    Thanks a lot Mr. Chihiro
    As for the values "A","B",etc .. I prefer to put them in one array
    x=Array("A","B","D","other")
    then to deal with those values either using arrays or collections (no matter) and finally got sub arrays for each item in x array ...
    Best Regards

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Split VBA array according to specific values

    Here's one way to do it. There are many other ways to do this.

    Sub Demo()
    Dim tempArr, x
    Dim condArr: condArr = Array("A", "B", "D", "Other")
    Dim dic As Object
    
    tempArr = Cells(1).CurrentRegion
    Set dic = CreateObject("Scripting.Dictionary")
    For i = 2 To UBound(tempArr)
        x = Application.Match(tempArr(i, 3), condArr, 0)
        If IsNumeric(x) Then
            dic(tempArr(i, 3)) = dic(tempArr(i, 3)) + 1
        Else
            tempArr(i, 3) = "Other"
            dic("Other") = dic("Other") + 1
        End If
    Next
    
    For Each Key In dic.Keys
        ReDim x(1 To dic(Key), 1 To 4)
        j = 1
        For i = 2 To UBound(tempArr)
            If tempArr(i, 3) = Key Then
                For k = LBound(tempArr, 2) To UBound(tempArr, 2)
                    x(j, k) = tempArr(i, k)
                Next
                j = j + 1
            End If
        Next
        dic.Remove Key
        dic.Add Key, x
    Next
    
    'Here you can loop through Key or access data some other way
    'Ex: Range("K1").Resize(UBound(dic("Other")), 4) = dic("Other")
    For Each Key In dic.Keys
        For i = LBound(dic(Key)) To UBound(dic(Key))
            For j = LBound(dic(Key), 2) To UBound(dic(Key), 2)
                Debug.Print dic(Key)(i, j)
            Next
        Next
    Next
    
    End Sub

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Split VBA array according to specific values

    That's really awesome and wonderful
    Thank you very very much Mr. Chihiro
    God bless you .. Have a nice time

+ 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. How to split numbers to specific name and total and split
    By ashokavarthanan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-13-2015, 09:12 AM
  2. [SOLVED] Need vba code with in 10line Split Into Separate Wkbooks Based on Values In a Specific Col
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2015, 02:56 PM
  3. Exluding specific values from an array function
    By kkitt in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-17-2014, 02:27 PM
  4. Need to use/convert values in specific range as/to an array
    By s4driver in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-18-2013, 06:46 AM
  5. [SOLVED] Split Array, Listing Marked Items in Split Arrays
    By lesoies in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-27-2013, 02:07 AM
  6. [SOLVED] How To Split Excel File Into Separate Workbooks Based on Values In a Specific Column
    By UzieJacuzzi in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 07-31-2012, 07:26 AM
  7. Store cell values in an array and return values on specific condition
    By gmalpani in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 11-27-2011, 06:43 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