Sorting Excel data

soumen21

New Member
Joined
Aug 16, 2019
Messages
35
Hi Experts,

I have a list of data in this pattern

1722663957225.png


I want to sort it in the following way.

1722664002423.png


Could you please help?

Best Regards
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Not sure if this is going to work but please test it:

Excel Formula:
=LET(
array1,D1:D10,
array2,B1:D10,
array3,A1:B10,
non,--(NOT(ISBLANK(array1))),
a,IF(non=1,array2,""),
b,DROP(REDUCE("",SEQUENCE(3),LAMBDA(c,d,HSTACK(c,SCAN("",CHOOSECOLS(a,d),LAMBDA(a,b,IF(b<>"",b,a)))))),,1),
c,CHOOSECOLS(FILTER(b,non=0),1,3,2),
d,CHOOSECOLS(FILTER(array3,non=0),2,1),
HSTACK(c,d))
 
Upvote 0
Data : Sheet1, A2:D10
Output: Sheet2, A2:E9
Macro code:
VBA Code:
Sub RearrangeData()
Dim A
'Dim K(1 To 3)
Dim T&, Ro&
A = Sheets("Sheet1").Range("A2").CurrentRegion
ReDim B(1 To UBound(A, 1), 1 To 5)
For T = 1 To UBound(A, 1) - 1
If A(T, 4) <> "" Then
ReDim K(1 To 3)
K(1) = A(T, 2): K(2) = A(T, 4): K(3) = A(T, 3)
Else
Ro = Ro + 1
B(Ro, 1) = K(1): B(Ro, 2) = K(2): B(Ro, 3) = K(3): B(Ro, 4) = A(T, 2): B(Ro, 5) = A(T, 1)
End If
Next T

With Sheets("Sheet2")
.Range("A2").Clear
.Range("A2").Resize(Ro, 5) = B
End With

End Sub
 
Upvote 0
Slight mistake was there. Corrected Macro code:
VBA Code:
Sub RearrangeData()
Dim A
'Dim K(1 To 3)
Dim T&, Ro&
A = Sheets("Sheet1").Range("A2").CurrentRegion
ReDim B(1 To UBound(A, 1), 1 To 5)
For T = 1 To UBound(A, 1)
If A(T, 4) <> "" Then
ReDim K(1 To 3)
K(1) = A(T, 2): K(2) = A(T, 4): K(3) = A(T, 3)
Else
Ro = Ro + 1
B(Ro, 1) = K(1): B(Ro, 2) = K(2): B(Ro, 3) = K(3): B(Ro, 4) = A(T, 2): B(Ro, 5) = A(T, 1)
End If
Next T

With Sheets("Sheet2")
.Range("A2").Clear
.Range("A2").Resize(Ro, 5) = B
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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