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...:)
 

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.
Try this slight tweak to the code that @Peter_SSs supplied here
https://www.mrexcel.com/forum/excel...-two-rows-when-condition-met.html#post4916446
Code:
Sub Two_Rows_v2()
  Dim FoundRow As Long
  Dim BreakData As Variant
  Dim i As Long
  
  Const BreakInfo As String = "0|Zero Score|N|Unconfirmed" '<- Add more pairs here if you want
  
  BreakData = Split(BreakInfo, "|")
  Application.ScreenUpdating = False
  For i = 0 To UBound(BreakData) Step 2
    FoundRow = 0
    On Error Resume Next
    FoundRow = Columns("C").Resize(, 2).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
Forgot the bit about column order.
Back in a mo
 
Upvote 0
Try
Code:
Sub Two_Rows_v2()
    Dim FoundRow As Long
    Dim BreakData As Variant
    Dim i As Long
    Dim Fnd As Range
    
    Const BreakInfo As String = "0|Zero Score|N|Unconfirmed" '<- Add more pairs here if you want
Application.ScreenUpdating = False
    
    Set Fnd = Rows(1).Find("Score", Range("A1"), , xlWhole, , , False, False)
    If Fnd.Column = 4 Then
        Columns(Fnd.Column).Cut
        Fnd.Offset(, -1).Insert
    End If
    
    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Columns(3), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=Columns(4), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("A:D")
            .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("C").Resize(, 2).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
Glad to help & thanks for the feedback
 
Upvote 0
One more thing as I forgot to mention it, what if there's a blank score and I want to put it under the "UNCONFIRMED" section. What's the code for that? Thanks again.
 
Upvote 0
How about
Code:
Sub Two_Rows_v2()
    Dim FoundRow As Long
    Dim BreakData As Variant
    Dim i As Long
    Dim Fnd As Range
    
    Const BreakInfo As String = "0|Zero Score|N|Unconfirmed" '<- Add more pairs here if you want
Application.ScreenUpdating = False
    
    Set Fnd = Rows(1).Find("Score", Range("A1"), , xlWhole, , , False, False)
    If Fnd.column = 4 Then
        Columns(Fnd.column).Cut
        Fnd.Offset(, -1).Insert
    End If
    
    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Columns(4), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=Columns(3), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("A:D")
            .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("C").Resize(, 2).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 actually works however if I try interchanging the "Score" next to "Name" and add some blank columns (until column Z). Some of the codes are not working.
Is there any code where we can sort it based on the Column name (basically Row A)? Thanks again for the help.
 
Upvote 0
Could you please supply a sample of what your data looks like?
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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