VBA to Sort and insert blank row based on criteria

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

Can you possibly help me out on the codes that I need to use? Newbie here..
I'm aiming to work on the following:
1. Sort by descending order and separate the Zero scores.
2. Next, sort it by Confirmation and separate the N (Confirmation) and add headers as well like those with "Y".

PS: The columns might be interchangeable so might be needing to be specific on the header name (Score, Confirmation etc.)


Before code execution.....

NameSportsScoreConfirmation
TaylorVolleyball10Y
BenBasketball20Y
StephenBaseball0Y
CaraVolleyball6N
EmmaTennis4N

<tbody>
</tbody>

After code execution:

NameSportsScoreConfirmation
BenBaskell20Y
TaylorVolleyball10Y
UNCONFIRMED
NameSportsScoreConfirmation
CaraVolleyball6N
EmmaTennis4N
ZERO SCORE
StephenBaseball0Y

<tbody>
</tbody>



Any help will be much appreciated...:)
 
Hi Fluff,

See below just consider there's a table for this. I've put "Score" next to "Name" and added new columns like Time, QTY, DAY and Game.
The column positions might differ sometimes but I'm using the same column name.


Name Score Sports Event Time QTY Confirmation DAY Game
Taylor 15 Volleyball 1 20m - Y 1 Morning
Cara 6 Volleyball 2 25m - N 1 Morning
Emma 4 Tennis 5 35m - N 2 Evening
Ben 10 Basketball 3 46m - Y 3 Evening
Step (blank) Baseball 4 26m - Y 4 Morning

Thanks again!
 
Last edited:
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
NameScoreSportsEventTimeQTYConfirmationDayGame
Taylor10Volleyball126m-Y1MORNING
Cara16Volleball139m-N1EVENING
Emma4Tennis246m-Y2EVENING
StephBaseball235m-Y2MORNING
Ben26Baskeball358m-Y3EVENING
Ron16Baskeball343m-Y3MORNING

<tbody>
</tbody>

Kindly use this table instead...thank you!
 
Upvote 0
This should work for the table above.
Code:
Sub Two_Rows_v2()
    Dim FoundRow As Long
    Dim BreakData As Variant
    Dim i As Long
    Dim ScCol As Range
    Dim CoCol As Range
    
    Const BreakInfo As String = "|Zero Score|N|Unconfirmed" '<- Add more pairs here if you want
Application.ScreenUpdating = False
    
    Set ScCol = Rows(1).Find("Score", Range("A1"), , xlWhole, , , False, False)
    Set CoCol = Rows(1).Find("Confirmation", Range("A1"), , xlWhole, , , False, False)
    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Columns(CoCol.column), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=Columns(ScCol.column), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("A:Z")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With

    BreakData = Split(BreakInfo, "|")
    For i = 0 To UBound(BreakData) Step 2
      FoundRow = 0
      On Error Resume Next
      FoundRow = Columns(ScCol.column).Resize(, 7).Find(what:=BreakData(i), LookIn:=xlValues, LookAt:=xlWhole, SearchFormat:=False).Row
      On Error GoTo 0
      If FoundRow > 0 Then
        Rows(FoundRow).Resize(2).Insert
        Cells(FoundRow + 1, 1).Value = BreakData(i + 1)
      End If
    Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hello,

I was working on some modifications on the macro that I created initially. I have below data which I need to group based on Status.

Before:

StatusNameScore
CurrentAna10
NotIncludedSusan20
ZeroBen0
NotConfirmedSean5
CurrentFlor30

<tbody>
</tbody>

With Code: In order = Current > Unconfirmed >Not Included > Zero

StatusNameScore
CurrentFlor30
CurrentAna10
UNCONFIRMED
StatusNameScore
NotConfirmedSean5
NOT INCLUDED
StatusNameScore
NotIncludedSusan20
NO SCORE
StatusName Score
ZeroBen0

<tbody>
</tbody>

*Each group must be sorted based on Name


Thanks in advance for all you help.
 
Upvote 0
Code:
Sub Two_Rows_v2()
    Dim FoundRow As Long
    Dim BreakData As Variant
    Dim i As Long
    Dim ScCol As Range
    Dim CoCol As Range
    
    Const BreakInfo As String = "NotConfirmed|Unconfirmed|notIncluded|Not Included|Zero|No Score" '<- Add more pairs here if you want
Application.ScreenUpdating = False
    
    Set ScCol = Rows(1).Find("Status", Range("A1"), , xlWhole, , , False, False)
    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Columns(ScCol.Column), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("A:Z")
            .header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With

    BreakData = Split(BreakInfo, "|")
    For i = 0 To UBound(BreakData) Step 2
      FoundRow = 0
      On Error Resume Next
      FoundRow = Columns(ScCol.Column).Resize(, 3).Find(what:=BreakData(i), LookIn:=xlValues, LookAt:=xlWhole, SearchFormat:=False).Row
      On Error GoTo 0
      If FoundRow > 0 Then
        Rows(FoundRow).Resize(2).Insert
        Cells(FoundRow + 1, 1).Value = BreakData(i + 1)
      End If
    Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
This is awesome Fluff!! This works well however the header for each group is missing - Status, Name & Score. Thanks again :)
 
Upvote 0
Ok, try this
Code:
Sub Two_Rows_v2()
    Dim FoundRow As Long
    Dim BreakData As Variant
    Dim i As Long
    Dim ScCol As Range
    Dim CoCol As Range
    
    Const BreakInfo As String = "NotConfirmed|Unconfirmed|notIncluded|Not Included|Zero|No Score" '<- Add more pairs here if you want
Application.ScreenUpdating = False
    
    Set ScCol = Rows(1).Find("Status", Range("A1"), , xlWhole, , , False, False)
    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Columns(ScCol.Column), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("A:Z")
            .header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With

    BreakData = Split(BreakInfo, "|")
    For i = 0 To UBound(BreakData) Step 2
      FoundRow = 0
      On Error Resume Next
      FoundRow = Columns(ScCol.Column).Resize(, 3).Find(what:=BreakData(i), LookIn:=xlValues, LookAt:=xlWhole, SearchFormat:=False).Row
      On Error GoTo 0
      If FoundRow > 0 Then
        Rows(FoundRow).Resize(3).Insert
        Cells(FoundRow + 1, 1).Value = BreakData(i + 1)
        Range("A1:C1").Copy Range("A" & FoundRow + 2)
      End If
    Next i
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,417
Messages
6,159,789
Members
451,589
Latest member
Harold14

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