VBA - example with arrays, checkboxes etc.

vbalad

New Member
Joined
Feb 3, 2023
Messages
8
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
Hi all,

I am a beginner in VBA.
In the example attached ( there are an unspecified number of codes and groups)
I need:
1. how to collect the codes belonging to a given group into an array or arrays, but into a range or ranges .
2. make columns for the groups in the new sheets created
3 . I need to assign a macro to the button "Check Checkboxes for Group" that will check the checkboxes in column A for a given group, as well as the possibility of selecting the group after entering in the InputBox

It seems to me that I need for point 1. an option to an array or arrays, because if I need to in new sheets columns for groups, I would like that within the workbook the group membership of the codes is remembered

Regards

1675423662475.png
 
Okay, so I have amended the code so that it places the results on the same worksheet as the source data, starting at E2...

VBA Code:
Option Explicit

Sub test()

    Const TextCompare As Long = 1

    Dim wsSource As Worksheet
    Set wsSource = ThisWorkbook.Worksheets("Sheet1") 'change the sheet name accordingly
    
    Dim lastRow As Long
    With wsSource
        lastRow = .Cells(.Rows.count, "C").End(xlUp).Row
    End With
    
    If lastRow < 2 Then
        MsgBox "No data found!", vbExclamation
        Exit Sub
    End If
    
    Dim Data As Variant
    Data = wsSource.Range("B2:C" & lastRow).Value
    
    'create a dictionary to store key/value pair
    'group stored as key, and array containing corresponding codes stored as value
    Dim dicCodesByGroup As Object
    Set dicCodesByGroup = CreateObject("Scripting.Dictionary")
    dicCodesByGroup.CompareMode = TextCompare
    
    'get a list of unique groups
    Dim i As Long
    For i = LBound(Data, 1) To UBound(Data, 1)
        dicCodesByGroup(Data(i, 2)) = ""
    Next i
    
    'for each group in the dictionary, get an array of corresponding codes, and add it to the dictionary
    Dim key As Variant
    Dim codes As Variant
    For Each key In dicCodesByGroup
        codes = getCodesByGroup(Data, key)
         dicCodesByGroup(key) = codes
    Next key
    
    'for each group in the dictionary, get the corresponding array, etc
    Dim key2 As Variant
    Dim group As String
    Dim codes2 As Variant
    Dim rw As Long
    rw = 2
    For Each key2 In dicCodesByGroup
        group = key2
        codes2 = dicCodesByGroup(key2)
        With wsSource
            .Range("E" & rw).Value = group
            .Range("F" & rw).Resize(, UBound(codes2, 1)).Value = codes2
        End With
        rw = rw + 1
    Next key2

End Sub

Private Function getCodesByGroup(ByRef Data As Variant, ByVal group As String) As Variant

    ReDim codes(1 To UBound(Data, 1)) As Long
    
    Dim count As Long
    Dim i As Long
    
    count = 0
    For i = LBound(Data, 1) To UBound(Data, 1)
        If LCase(Data(i, 2)) = LCase(group) Then 'case-insensitive matching
            count = count + 1
            codes(count) = Data(i, 1)
        End If
    Next i
    
    If count > 0 Then
        ReDim Preserve codes(1 To count)
    End If
    
    getCodesByGroup = codes()
    
End Function

Before . . .

vbalad.xlsm
ABCDEFGHIJKLM
1CodeGroupResults
21D
32B
43B
54A
65C
76C
87B
98C
109D
1110B
1211A
1312D
1413C
1514B
1615D
1716B
1817A
1918B
2019A
Sheet1


After . . .

vbalad.xlsm
ABCDEFGHIJKLM
1CodeGroupResults
21DD191215
32BB23710141618
43BA4111719
54AC56813
65C
76C
87B
98C
109D
1110B
1211A
1312D
1413C
1514B
1615D
1716B
1817A
1918B
2019A
Sheet1


Hope this helps!
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
@Domenic
Thanks a lot !!! Works perfectly :)

Meanwhile, since I do not know how to filter 2D arrays, came up with the following ...

Could you kindly review and let me have your comments
VBA Code:
Sub AB_Test()
'https://www.mrexcel.com/board/threads/vba-example-with-arrays
Dim arr As Variant, codes As Variant
Dim key As Variant, it As Variant
Dim i As Long, lcount As Long, ext As String

lcount = 1
With Sheet1
  arr = .Range("B2", .Range("C" & Rows.count).End(xlUp))
End With

    With CreateObject("Scripting.Dictionary")
        ' Create Dictionary
        For Each it In Application.index(arr, 0, 2)
            .Item(it) = it
        Next it
        ' Loop Dictionary to create Codes
        For Each key In .Keys
          ext = "": lcount = lcount + 1
            For i = 1 To UBound(arr)
                If arr(i, 2) = key Then
                    ext = ext & "_" & i
                    codes = Application.index(arr, Application.Transpose(Split(Mid(ext, 2), "_")), Array(1, 2))
                    'Display Final Results for Each Key
                    Cells(lcount, 6) = key
                    Cells(lcount, 7).Resize(, UBound(codes)) = Application.Transpose(codes)
                 End If
            Next i
        Next key
    End With
End Sub
 
Upvote 0
My comment has very little importance ....

The OP *vbalad is the person whose opinion really counts :)
 
Upvote 0
Actually, your comments are important as well . . .

When you posted your code, was that meant for the OP? I thought it was a new question that you needed help with. Sorry if I misunderstood.
@ Domenic

My attempt was indeed posted for the OP ... but ALSO for your sharpened review ;)
 
Upvote 0
@James006

Sorry again for my misunderstanding.

James, that's a very interesting approach. There's only one minor issue, though. It occurs when there's only one code for a group. Here's what I mean...

vbalad.xlsm
ABCDEFGHIJKLMN
1CodeGroupResults
21C12C11
32AA258
43DD361418
54BB4791011151719
65AE121316
76D
87B
98A
109B
1110B
1211B
1312E
1413E
1514D
1615B
1716E
1817B
1918D
2019B
Sheet1


Cheers!
 
Upvote 0
Hi all,
Thank you very much for your help. I haven't had time to check yet because I had to take care of another task . I will come back with feedback.
Regards
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,770
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top