Converting one long column into multiple columns based on specific variable name in column

raynejan

New Member
Joined
Nov 27, 2013
Messages
2
Hello,
I have a very large dataset (i.e., >10k rows) which contains two columns: the first column contains the variables of interest and the second the values corresponding to those variables. These variables are repeated several times over one column (rather than split up). To demonstrate, I have included a screenshot of a pseudo dataset in which we have several variables of interest (named as A to G) in the first column with their respective values in the second column.

What I am hoping to do is split the long two columns every time the variable "A" appears. I've seen suggestions in the forum where you split based on the number of rows (i.e., split every 15th row, for example). The reason I haven't applied this is because sometimes the column contains more than the initial variables of interest. For example, rather than having A to G, you might have A to M appear in some instances along the column. Therefore, ideally, I'd like to split the large column into smaller columns every time the variable "A" appears.

Any recommendations would be greatly appreciated. Thank you in advance!



example_output.png
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Power Query Solution

Book18
ABCDEFGH
2A0.293177A0.2931770.24416693
3B1.063529B1.0635290.55622151
4c1.101265D0.2441660.0353281.910847
5D0.244166E0.311802283.170230
6E0.311802F3.1492070.0390332.839609
7F3.149207G12.8978117.17765197
8G12.89781c1.1012650.05989725309149
9A0.244166
10B0.55622
11c0.059897
12D0.035328
13E283.1702
14F0.039033
15G17.17765
16A93
17B151
18c25309149
19D1.910847
20E30
21F2.839609
22G197
Sheet1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Column1"}, {{"Data", each _, type table [Column1=text, Column2=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Data],"Index",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Column2", "Index"}, {"Custom.Column2", "Custom.Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Custom", {{"Custom.Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expanded Custom", {{"Custom.Index", type text}}, "en-US")[Custom.Index]), "Custom.Index", "Custom.Column2")
in
    #"Pivoted Column"
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)


Also better if you can give us sample data in a form that we can copy from to test with. :)
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.


If you wanted to try a macro approach, give this a go with a copy of your data.

VBA Code:
Sub Split_To_Cols()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, blocksize As Long, col As Long

  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value
  blocksize = Columns("A").Find(What:=Range("A1").Value, LookAt:=xlWhole).Row - 1
  ReDim b(1 To blocksize, 1 To UBound(a) / blocksize * 2)
  col = -1
  For i = 1 To UBound(a) Step blocksize
    col = col + 2
    For j = 1 To blocksize
      b(j, col) = a(i + j - 1, 1)
      b(j, col + 1) = a(i + j - 1, 2)
    Next j
  Next i
  Range("E1").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0
@Peter_SSs
I just found a new feature in O365 Insider that allows you to import a picture into a spreadsheet. I used in this particular case. Thought you might be interested. Here is the link

 
Upvote 0
Hi Alan
I assume that you mentioned that because I had asked for an XL2BB sample.
Yes, I was already aware of the new feature you mentioned and have done quite a few test with it. Whilst it is not bad it has not been 100% reliable in recognising characters for me so I would still rather the reliability of XL2BB where possible.
 
Upvote 0
Peter,
I concur with you on the XL2BB. I prefer it. Just trying to be helpful.

Alan
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

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