Code to help reorganise data

ellison

Active Member
Joined
Aug 1, 2012
Messages
356
Office Version
  1. 365
Platform
  1. Windows
Hi, hoping that somebody may be able to help with some code to help reorganise some info...

Please see raw data on the left: there are 2 columns of data plus a helper column.

On the right is the (hopefully)! reorganised data.

Effectively, we would like one single row of data for each Line-ID on the reorganised version. And to have the different Part Numbers going across the columns (from H to L).

Book2
ABCDEFGHIJKL
1Raw-DataLine-IDPart-NumberHelper: OccurrenceReorganised-DataLine IDPN-1PN-2PN-3PN-4PN-5
219231ABC/1231st19231ABC/1235962-9954LS_TCXMC54hcMC54hc
3192315962-992nd19232ABC/123Sn54LSMC54LS
419231ABC/1233rd19233123456
519231MC54hc4th19234
619231MC54hc5th
719232XYZ78-91st
819232Sn54LS2nd
919232MC54LS3rd
10192331234561st
1119234
Sheet1


Just to add to the complications (oops!), we'd like to this to work with

- larger sets of raw data (up to 100K rows)
- we'd like the Part Numbers to keep their upper and lower case characters
NB the Part Numbers themselves can be alpha (a-z or A-Z), numeric (0-9) or alphanumeric and also contain other characters (dashes, slashes, space etc)
- sometimes the Part Numbers can be blank

Hope this helps.

Huge thanks for any help!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Will your line-ID always be sorted together?
 
Upvote 0
Ok, how about
VBA Code:
Sub ellison()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, nc As Long
   
   With Sheets("sheet1")
      Ary = .Range("B1:C" & .Range("B" & Rows.Count).End(xlUp).Row).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 1000)
   For r = 2 To UBound(Ary)
      If Ary(r, 1) <> Ary(r - 1, 1) Then
         nr = nr + 1
         nc = 2
         Nary(nr, 1) = Ary(r, 1)
         Nary(nr, 2) = Ary(r, 2)
      Else
         nc = nc + 1
         Nary(nr, nc) = Ary(r, 2)
      End If
   Next r
   Sheets("sheet1").Range("G2").Resize(nr, UBound(Nary, 2)).Value = Nary
End Sub
 
Upvote 0
Solution
Oh wow that is absolutely amazing...........Huge thanks, solution found!!!

Now that I think about it, would it be possible to tweak the macro to include a 3rd column in the raw data?

(I took the helper column out and replaced it with a mfr column which would be great to include in the reorganised data - if poss!)

Something like this:

bounce-back-macro-help.xlsm
ABCDEFGHIJKLMNOPQ
1Raw-DataLine-IDPart-NumberMfrReorganised-DataLine IDPN-1Mfr-1PN-2Mfr-2PN-3Mfr-3PN-4Mfr-4PN-5Mfr-5
219231ABC/123Philips19231ABC/123Philips5962-99Fujitsu54LS_TCXSamsungMC54hcTIMC54HcTexas
3192315962-99Fujitsu19232ABC/123FreescaleSn54LSMC54LSIBM
419231ABC/123Samsung19233123456Hitachi
519231MC54hcTI19234
619231MC54HcTexas
719232XYZ78-9Freescale
819232Sn54LS
919232MC54LSIBM
1019233123456Hitachi
1119234
Sheet1
 
Upvote 0
How about
VBA Code:
Sub ellison()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, nc As Long
   
   With Sheets("sheet1")
      Ary = .Range("B1:D" & .Range("B" & Rows.Count).End(xlUp).Row).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 1000)
   For r = 2 To UBound(Ary)
      If Ary(r, 1) <> Ary(r - 1, 1) Then
         nr = nr + 1
         nc = 2
         Nary(nr, 1) = Ary(r, 1)
         Nary(nr, 2) = Ary(r, 2)
         Nary(nr, 3) = Ary(r, 3)
      Else
         nc = nc + 2
         Nary(nr, nc) = Ary(r, 2)
         Nary(nr, nc + 1) = Ary(r, 3)
      End If
   Next r
   Sheets("sheet1").Range("G2").Resize(nr, UBound(Nary, 2)).Value = Nary
End Sub
 
Upvote 0
.....WOW!!!!!!!!!!!

Thank you very much indeed. That works beautifully.

And works beautifully on a 100K set of data too!

Absolutely brilliant, very thankful indeed.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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