Split in to multiple columns based on row value

mattdavid.hall

New Member
Joined
Oct 2, 2008
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hello,

We have an interesting situation where an old system outputs data in to a CSV that looks as shown in this image:

sample.png
csv.png


We need to split this in to multiple columns. It's hard for me to easily describe the end result, so here is a picture of what we want:

desired format.png


I can easily take an excel file and create some IF statements to move data over to column F-T, but I don't know if I can, or how to automatically delete the blank cells and move the data UP as shown.

Are there any easy ways that I can do what we're looking for?

Thanks as always!

 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
if your data is in Sheet1 ( Otherwise Change Sheet1 at code to your Sheet Name) and Start from Row 1, Try this and see result at Sheet2 :
VBA Code:
Sub Test()
Dim i As Long, Lr As Long, Ws1 As Worksheet, Ws2 As Worksheet, K As Long, Lr2 As Long
Set Ws1 = Sheets("Sheet1")
On Error Resume Next
Set Ws2 = Sheets("Sheet2")
If Err.Number = 9 Then
Worksheets.Add After:=ActiveSheet
Set Ws2 = ActiveSheet
End If
On Error GoTo 0
Lr = Ws1.Range("A" & Rows.Count).End(xlUp).Row
Ws1.Range("A1:A" & Lr).TextToColumns Destination:=Ws1.Range("A1"), DataType:=xlDelimited, Comma:=True
If Range("D1").Value <> "" Then Columns("B:B").EntireColumn.Delete
Ws1.Range("D1").Value = "Distinct"
Ws1.Range("D2:D" & Lr).Formula = "=IFERROR(INDEX($B$1:$B$" & Lr & ",MATCH(0,INDEX(COUNTIF($D$1:D1,$B$1:$B$" & Lr & "),0,0),0)),"""")"
For i = 1 To Lr
K = Application.WorksheetFunction.Match(Ws1.Range("B" & i), Ws1.Range("D2:D" & Lr), 0)
Lr2 = Ws2.Cells(Rows.Count, K * 3).End(xlUp).Row + 1
Range(Ws2.Cells(Lr2, K * 3 - 2), Ws2.Cells(Lr2, K * 3)).Value = Ws1.Range("A" & i & ":C" & i).Value
Next i

End Sub
 
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’)
 
Upvote 0
Sub Test() Dim i As Long, Lr As Long, Ws1 As Worksheet, Ws2 As Worksheet, K As Long, Lr2 As Long Set Ws1 = Sheets("Sheet1") On Error Resume Next Set Ws2 = Sheets("Sheet2") If Err.Number = 9 Then Worksheets.Add After:=ActiveSheet Set Ws2 = ActiveSheet End If On Error GoTo 0 Lr = Ws1.Range("A" & Rows.Count).End(xlUp).Row Ws1.Range("A1:A" & Lr).TextToColumns Destination:=Ws1.Range("A1"), DataType:=xlDelimited, Comma:=True If Range("D1").Value <> "" Then Columns("B:B").EntireColumn.Delete Ws1.Range("D1").Value = "Distinct" Ws1.Range("D2:D" & Lr).Formula = "=IFERROR(INDEX($B$1:$B$" & Lr & ",MATCH(0,INDEX(COUNTIF($D$1:D1,$B$1:$B$" & Lr & "),0,0),0)),"""")" For i = 1 To Lr K = Application.WorksheetFunction.Match(Ws1.Range("B" & i), Ws1.Range("D2:D" & Lr), 0) Lr2 = Ws2.Cells(Rows.Count, K * 3).End(xlUp).Row + 1 Range(Ws2.Cells(Lr2, K * 3 - 2), Ws2.Cells(Lr2, K * 3)).Value = Ws1.Range("A" & i & ":C" & i).Value Next i End Sub

Thank you for the help!
When we run this script we receive an error "Method 'Range' of object '_Worksheet' failed"

The script creates Sheet2 but it is blank. It does created the Distinct column on Sheet1 with the distinct values from Column B and it is accurate. I'll dig in to this code today and see if I can figure out what's going on. I do appreciate the direction!
 
Upvote 0
Updated, thank you!
Thanks for that.

Since yo have 365, could you use something like this?
The formula in F2 is copied and pasted into I2, L2, O2 and R2 only

mattdavid.hall.xlsm
ABCDEFGHIJKLMNOPQRST
1REGOTVACREG3REG2
22495VAC48.082340REG12.92367OT19.372495VAC48.082386REG313.272480REG226.82
32340REG12.92323REG26.22496OT25.752337VAC12.612422REG318.792419REG210.62
42367OT19.372442REG19.512326OT25.12443VAC10.352351REG337.792334REG249.16
52386REG313.272421REG46.62423VAC19.422386REG212.87
62480REG226.822374REG240.89
72337VAC12.612476REG221.22
82419REG210.62
92443VAC10.35
102334REG249.16
112323REG26.2
122496OT25.75
132442REG19.51
142421REG46.6
152386REG212.87
162374REG240.89
172422REG318.79
182326OT25.1
192476REG221.22
202423VAC19.42
212351REG337.79
22
Sheet1
Cell Formulas
RangeFormula
F2:H5,R2:T7,O2:Q4,L2:N5,I2:K4F2=INDEX(FILTER($A$2:$D$21,$C$2:$C$21=F1),SEQUENCE(COUNTIF($C$2:$C$21,F1)),{1,3,4})
Dynamic array formulas.
 
Upvote 0
Solution
RangeFormula
Cell Formulas
F2:H5,R2:T7,O2:Q4,L2:N5,I2:K4F2=INDEX(FILTER($A$2:$D$21,$C$2:$C$21=F1),SEQUENCE(COUNTIF($C$2:$C$21,F1)),{1,3,4})
Dynamic array formulas.

Peter, that is awesome and it worked perfectly!

Thank you so much my dude!
 
Upvote 0
You're welcome. Thanks for the follow-up. :)

BTW, if you have the LET function you can write the existing formula ..
Excel Formula:
=INDEX(FILTER($A$2:$D$21,$C$2:$C$21=F1),SEQUENCE(COUNTIF($C$2:$C$21,F1)),{1,3,4})

.. marginally more compactly as
Excel Formula:
=LET(f,FILTER($A$2:$D$21,$C$2:$C$21=F1),INDEX(f,SEQUENCE(ROWS(f)),{1,3,4}))
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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