Creating a macro to move info in a column to rows

cmaronde

New Member
Joined
May 31, 2012
Messages
7
Hello, I am trying to figure out how to move data from columns to a row. The following data comes like the example below, with Data1 being the first grouping, data2 is the second grouping and this needs to keep repeating until there is no more data (the only info I need is the data in column A, the rest can be discarded):
Data1-1
Data1-2
Data1-3
Data1-4
Data1-5
Data1-6
Data1-7
Data2-1
Data2-2
Data2-3
Data2-4
Data2-5
Data2-6Data2-7

I want it to come out in 1 row:
Data1-1Data1-3Data1-5
Data2-1Data2-3Data2-5

Thanks for any help!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You can do this with Power Query.

Book1
ABCDEFG
2Data1-1
3Data1-2
4Data1-3
5Data1-4
6Data1-5
7Data1-6Data1-7
8Data2-1
9Data2-2
10Data2-3
11Data2-4
12Data2-5
13Data2-6Data2-7
14
15
16
17Custom.1.1Custom.1.2Custom.1.3Custom.1.4Custom.1.5Custom.1.6Custom.1.7
18Data1-1Data1-3Data1-5Data1-2Data1-4Data1-6Data1-7
19Data2-1Data2-3Data2-5Data2-2Data2-4Data2-6Data2-7
Sheet2


Code:
let
    Source = List.RemoveNulls(List.Combine(Table.ToColumns(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]))),
    ToTable = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    GroupKey = Table.AddColumn(ToTable, "Custom", each Text.Middle([Column1],0,5)),
    Group = Table.Group(GroupKey, {"Custom"}, {{"Count", each _, type table}}),
    GetList = Table.AddColumn(Group, "Custom.1", each Table.Column([Count],"Column1")),
    Extract = Table.TransformColumns(GetList, {"Custom.1", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    Split = Table.SplitColumn(Extract, "Custom.1", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Custom.1.1", "Custom.1.2", "Custom.1.3", "Custom.1.4", "Custom.1.5", "Custom.1.6", "Custom.1.7"}),
    RC = Table.RemoveColumns(Split,{"Custom", "Count"})
in
    RC
 
Upvote 0
Thanks for the reply lrobbo314 but unfortunately, I have Excel 2013, which does not have power query and as this is a work laptop, I cannot download it. Any other thoughts?
 
Upvote 0
Give this a shot.

VBA Code:
Sub FLATTEN()
Dim r As Range:         Set r = Range("A1:G12")
Dim AR() As Variant:    AR = r.Value
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")

Dim RO As Integer, CO As Integer
Dim x As Variant, y As Variant
Dim ID As String

For i = 1 To UBound(AR)
    For j = 1 To UBound(AR, 2)
        If Not IsEmpty(AR(i, j)) Then
            ID = Left(AR(i, j), 5)
            SD(ID) = SD(ID) + 1
        End If
    Next j
Next i

r.ClearContents
x = SD.items
y = SD.keys

For RO = 1 To UBound(y) + 1
    For CO = 1 To x(RO - 1)
        Cells(RO, CO) = y(RO - 1) & "-" & CO
    Next CO
Next RO

End Sub
 
Upvote 0
When I ran it, it only worked for a but then stopped (row 13 is the start of the 3rd name) but also shortened the names and added -1.
1582236739880.png
 
Upvote 0
If you look at the code you can see where it is getting the left 5 characters of each cell. That was based off of your sample data in your OP, e.g. 'Data-1', 'Data-2', etc. Looking at you last post, that data is way different that the sample.

Can you post your actual data and how you want it to look after the code is run.
 
Upvote 0
That's a fine sample size. And how do you want it to look after the code is run? Just a sample of the first few should be ok.
 
Upvote 0
So, whichever is easier to program:
1582259954930.png


or just names and email addresses as that's truly all I need:
1582259988137.png


I really appreciate the help on this!
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,520
Members
448,968
Latest member
Ajax40

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