VBA: Simple data arrangement issue

icesanta

Board Regular
Joined
Dec 17, 2015
Messages
65
Please help me with this simple data arrangement issue.
If data is found in the cell then only the VBA will work.
I have around 50 thousand rows.
I have the data in sheet 1 and need to get the data in sheet 2 as shown in the image.
Thanks in advance.
 

Attachments

  • arrange data.png
    arrange data.png
    29.4 KB · Views: 15

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Two things that might help in the future:

1. 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’)

2. 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.


Assuming that Sheet2 exists in your workbook but is empty, try this.

VBA Code:
Sub Rearrange()
  Dim a As Variant, b As Variant, Bits As Variant
  Dim i As Long, j As Long, k As Long, c As Long, uba2 As Long
  
  With Sheets("Sheet1")
    a = .Range("A1").CurrentRegion.Value
    uba2 = UBound(a, 2)
  End With
  ReDim b(1 To Rows.Count, 1 To uba2)
  For i = 1 To UBound(a)
    Bits = Split(a(i, 2), ";")
    For c = 0 To UBound(Bits)
      k = k + 1
      b(k, 1) = a(i, 1): b(k, 2) = Bits(c)
      For j = 3 To uba2
        b(k, j) = a(i, j)
      Next j
    Next c
  Next i
  With Sheets("Sheet2").Range("A1").Resize(k, uba2)
    .Value = b
    .Columns.AutoFit
  End With
End Sub
 
Upvote 0
VBA Code:
Option Explicit
Sub test()
Dim Lr&, i&, j&, k&, cell As Range, s, arr()
With Sheets("Sheet1")
Lr = .Cells(Rows.Count, "A").End(xlUp).Row
ReDim arr(1 To 65000, 1 To 8)
    For Each cell In .Range("B3:B" & Lr)
        s = Split(cell, ";")
        For i = 0 To UBound(s)
            k = k + 1
            For j = -1 To 6
                arr(k, j + 2) = cell.Offset(0, j).Value
                If j = 0 Then arr(k, j + 2) = s(i)
            Next
        Next
    Next
End With
With Sheets("Sheet2")
    Sheets("Sheet1").Range("A2:H2").Copy .Range("A2")
    .Range("A3").Resize(k, 8).Value = arr
    .Range("A3").Resize(k, 8).Borders.LineStyle = xlContinuous
End With
End Sub
BEFORE:
Book1
ABCDEFGH
1
2GroupNameSubjectMarksPCOC
3GreenDavid;MattMatchs-10afgusd
4GreenGoliaMatchs-20bghjjadf
5RedGoliaMatchs-20cfggh
6BlueDavid;Matt;HenryEnglish0.0001fghggdfg
7BlueDanishEnglish-0.0002egfhdfsdf
Sheet1

AFTER:
Book1
ABCDEFGH
2GroupNameSubjectMarksPCOC
3GreenDavidMatchs-10afgusd
4GreenMattMatchs-10afgusd
5GreenGoliaMatchs-20bghjjadf
6RedGoliaMatchs-20cfggh
7BlueDavidEnglish0.0001fghggdfg
8BlueMattEnglish0.0001fghggdfg
9BlueHenryEnglish0.0001fghggdfg
10BlueDanishEnglish-0.0002egfhdfsdf
Sheet2
 
Upvote 0
Thank you, Bebo. Can I get the rows sorted in descending order of marks by subject (one below another)?
And get the least ten in sheet3 in descending order of marks by subject (one below another)?
 
Upvote 0
Did you try the other code?
For large data it should be quicker and you did say that you had around 50,000 rows.

Can I get the rows sorted in descending order of marks by subject (one below another)?
Like this?

VBA Code:
Sub Rearrange_v2()
  Dim a As Variant, b As Variant, Bits As Variant
  Dim i As Long, j As Long, k As Long, c As Long, uba2 As Long
 
  With Sheets("Sheet1")
    a = .Range("A1").CurrentRegion.Value
    uba2 = UBound(a, 2)
  End With
  ReDim b(1 To Rows.Count, 1 To uba2)
  For i = 1 To UBound(a)
    Bits = Split(a(i, 2), ";")
    For c = 0 To UBound(Bits)
      k = k + 1
      b(k, 1) = a(i, 1): b(k, 2) = Bits(c)
      For j = 3 To uba2
        b(k, j) = a(i, j)
      Next j
    Next c
  Next i
  Application.ScreenUpdating = False
  With Sheets("Sheet2").Range("A1").Resize(k, uba2)
    .Value = b
    .Columns.AutoFit
    .Sort Key1:=.Columns(3), Order1:=xlAscending, _
          Key2:=.Columns(4), Order2:=xlDescending, _
          Header:=xlYes
    .Parent.Activate
  End With
  Application.ScreenUpdating = True
End Sub

And get the least ten in sheet3 in descending order of marks by subject (one below another)?
You would need to explain that more fully and/or give some small sample data and the results you mean by this.
 
Upvote 0
Thank you.
In column B I have names and in C I have subjects
I want to sort the marks data in descending order considering the name and subject both as in the image.
I want to check the lowest to highest in each subject and want to know their names but only the lowest 10.
 

Attachments

  • Screenshot 30-03-2022 125346.png
    Screenshot 30-03-2022 125346.png
    45.4 KB · Views: 6
Last edited:
Upvote 0
So, did my latest code produce the Sheet2 results that you wanted? (No point in proceeding if the first part is not what you want)
 
Upvote 0
My apologies.
As there are more than 50 thousand rows so it is not easy for me to know the lowest 10 of say, David or Goliath. There are hundreds of names.
And I want to know the lowest 10 of each name in each subject.
Hope I am clear now.
 
Upvote 0
For testing purpose, it's better if you can re-post new version, with more rows, to display the lowest 10.
 
Upvote 0
As there are more than 50 thousand rows so it is not easy for me to know the lowest 10 of say, David or Goliath. There are hundreds of names.
And I want to know the lowest 10 of each name in each subject.
Hope I am clear now.
No, it is not clear as you did not address my last question..
So, did my latest code produce the Sheet2 results that you wanted? (No point in proceeding if the first part is not what you want)
 
Upvote 0

Forum statistics

Threads
1,215,267
Messages
6,123,964
Members
449,137
Latest member
yeti1016

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