VBA - trying to extract data and show it in two dimension

LBee

New Member
Joined
Dec 25, 2021
Messages
20
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi all

I'm trying to create at macro, that makes data more readable by creating a two-dimensional view, but I'm so stuck.
Using data from examples found on differeny webpages, I mange to either have data from column a in rows or data from column B in columns, but I have no idea how to combine those

I have made a simple example, to illustrate what data looks like, and what my goal is

Data looks like this

Customer Product
Customer1a
Customer1c
Customer2a
Customer2d
Customer2e
Customer2f
Customer3a
Customer3b
Customer3c
Customer3d
Customer3e
Customer4a
Customer4e


And my goal is to create a new sheet looking like this :
Customer1ac
Customer2adef
Customer3abcde
Customer4ae


Does it make sense and if yes, is it even possible to do what I'm trying to do?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I do not know how big bunch of data you are dealing with, but maybe you can use formulas instead of macro:

Book1
ABCDEFGHI
1Customer Product
2Customer1aCustomer1ac
3Customer1cCustomer2adef
4Customer2aCustomer3abcde
5Customer2dCustomer4ae
6Customer2e
7Customer2f
8Customer3a
9Customer3b
10Customer3c
11Customer3d
12Customer3e
13Customer4a
14Customer4e
Sheet1
Cell Formulas
RangeFormula
D2:D5D2=UNIQUE($A$2:$A$14)
E2:F2,E5:F5,E4:I4,E3:H3E2=TRANSPOSE(FILTER($B$2:$B$14,$A$2:$A$14=$D2))
Dynamic array formulas.
 
Upvote 0
Hi
I'm trying to create at macro
Try
VBA Code:
Sub test()
    Dim a
    Dim i&
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    a = Range("a2:a" & Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 2)
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a)
            If a(i, 1) <> 0 Then
                If Not .exists(a(i, 1)) Then
                    .Add a(i, 1), a(i, 2)
                Else
                    .Item(a(i, 1)) = .Item(a(i, 1)) & "," & a(i, 2)
                End If
            End If
        Next
        a = Application.Index(Array(.keys, .items), 0, 0)
        Cells(2, 4).Resize(.Count, 2) = Application.Transpose(a)
        Columns("E:E").TextToColumns Destination:=Range("E1"), Comma:=True, FieldInfo:=Array(Array(1, 1))
    End With
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Thanks a lot for the answersm, both of you.
The amount of data could get relatively big, and if it was possible, I would prefer to do it with a macro (which it apparently was :) ), but I always love to see different approaches to the same problem.

I can confirm that the macro is working, but I also now realise that if forgot to mention, that my goal was to have the output in a separate sheet, which would have been easier to see if I had used xl2bb table instead of using mini sheet (learning a lot today) .

Would it be possible to send the output a sepereate sheet instead?
 
Upvote 0
that my goal was to have the output in a separate sheet
Then try
VBA Code:
Sub test()
    Dim a
    Dim i&
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    With Sheets("sheet1") '<< Change  source sheet name as requiered
    a = .Range("a2:a" & .Cells(Rows.Count, 1).End(xlUp).Row).Resize(, 2)
    End With
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a)
            If a(i, 1) <> 0 Then
                If Not .exists(a(i, 1)) Then
                    .Add a(i, 1), a(i, 2)
                Else
                    .Item(a(i, 1)) = .Item(a(i, 1)) & "," & a(i, 2)
                End If
            End If
        Next
        a = Application.Index(Array(.keys, .items), 0, 0)
        End With
        With Sheets("sheet2") '<< Change  Destination sheet name as requiered
        .Cells(2, 1).Resize(UBound(a, 2), 2) = Application.Transpose(a)
        .Columns("B:B").TextToColumns Destination:=.Range("B1"), Comma:=True, FieldInfo:=Array(Array(1, 1))
    End With
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
Hi mohadin

I can confirm it working (with a minor adjustment) 🥳
In this line I had to replace B1 with B2, otherwise the output was messing up a bit (but that was an easy fix)
VBA Code:
.Columns("B:B").TextToColumns Destination:=.Range("B1"), Comma:=True, FieldInfo:=Array(Array(1, 1))

Now I need to dig in was the "CreateObject("scripting.dictionary")" part actually does, I guess thats where the magic happens :)

Thanks a lot for you help 🙏
 
Upvote 0
You are very welcome LBee
Glad I could help
Thank you for the feedback
Be happy and safe
 
Upvote 0

Forum statistics

Threads
1,215,162
Messages
6,123,382
Members
449,097
Latest member
Jabe

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