Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have values in one sheet mentioned below. Col A has some Group names. Column B has Demo Values.

Col A	Col B
member{"cn=Group_A,OU=AA,DC=BB}", "cn=Group_B, OU=AB, DC=BB}", "cn=Group_C,OU=AA,DC=BB}"	Demo1
member {"cn=Group_B, OU=AA, DC=BB"}	Demo2
member {"cn=Group_A, OU=AA, DC=BB"}	Demo3
member {"cn=Group_C, OU=AA, DC=BB"}	Demo4
member {"cn=Group_D, OU=AA, DC=BB"}	Demo5


I want to separate the values and expecting the output mentioned below:

Col A	Col B
Group_A	Demo1, Demo3
Group_B	Demo1, Demo2
Group_C	Demo1, Demo4
Group_D	Demo5


What I have tried:

I tried to perform substring to match with the text and the tried to perform text join but it's not successful. Can anyone please help? This is really urgent.
Posted
Updated 2-Apr-24 11:41am
v2
Comments
0x01AA 2-Apr-24 14:00pm    
I would say: No chance without VBA
Maciej Los 2-Apr-24 16:43pm    
You're right, Bruno :)
webmail123 2-Apr-24 17:43pm    
Thanks for the solution. I really appreciate it. I just tweaked the value of column A. So we can ignore all other values in column A except value of "CN". How can I make that change in VBA Code? I tried to replace it with empty string but if you observe, values of OU and DC can be different for each row. But we just need to consider the value of CN
Maciej Los 3-Apr-24 13:50pm    
You changed sample data. I'd suggest to use Replace function. Change my code to your needs :)
webmail123 3-Apr-24 13:51pm    
I will do that. Thank you so much!!

1 solution

I'd suggest to use a dictionary object. See:

vba
Option Explicit

'needs reference to Mictosoft Scripting Runtime library
Sub RevertData()
    Dim srcWsh, dstWsh As Worksheet
    Dim i, k  As Long
    Dim sTmp As String
    Dim sValues As Variant
    Dim myDictionary As Dictionary
    
    'create dictionary object
    Set myDictionary = New Dictionary
    
    'define source sheet
    Set srcWsh = ThisWorkbook.Worksheets("Sheet1")
    'define destinate sheet
    Set dstWsh = ThisWorkbook.Worksheets("Sheet2")
    'start with row no. 2
    'counter - sheet 1
    i = 2
    Do While srcWsh.Range("A" & i) <> ""
        sValues = Split(srcWsh.Range("A" & i), ", ")
        For k = LBound(sValues) To UBound(sValues)
            'Debug.Print CStr(sValues(k))
            sTmp = sValues(k)
            sTmp = Replace(sTmp, "cn=", "")
            sTmp = Replace(sTmp, """", "")
            If Not myDictionary.Exists(sTmp) Then
                myDictionary.Add sTmp, CStr(srcWsh.Range("B" & i)) & ", "
            Else
                myDictionary(sTmp) = myDictionary(sTmp) & CStr(srcWsh.Range("B" & i)) & ", "
            End If
        Next k
        'increase counter
        i = i + 1
    Loop
    
    'counter - sheet 2
    i = 2
    For Each sValues In myDictionary.Keys
        dstWsh.Range("A" & i) = CStr(sValues)
        dstWsh.Range("B" & i) = myDictionary(CStr(sValues))
        i = i + 1
    Next
    
    Set dstWsh = Nothing
    Set srcWsh = Nothing
    
End Sub
 
Share this answer
 
Comments
0x01AA 3-Apr-24 2:06am    
My 5 ;)
Maciej Los 3-Apr-24 13:47pm    
Thank you, Bruno :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900