VBA only sort if there is data

Isabella

Well-known Member
Joined
Nov 7, 2008
Messages
643
Hi, i need a condition in the below code to only proceed to sort if there is data from row 6 onwards else exit sub

Code:
Sub Test()

Dim lDstRowNum As Long
    With Sheets(1)
        lDstRowNum = .Cells(.Rows.Count, "A").End(xlUp).Row
        If lDstRowNum > 5 Then
            With .Range("P6:P" & lDstRowNum)
                .Formula = "=MATCH(A6,rngSecSort,0)"
            End With
            .Range(gGroupHeader).CurrentRegion.Sort Key1:=.Range("P6"), Order1:=xlAscending, _
            Key2:=.Range("E6"), Order2:=xlAscending, Key3:=.Range("G6"), Order3:=xlDescending, Header:=xlGuess, _
            ordercustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
        End If
        With .Range("P6:P" & lDstRowNum)
            .ClearContents
        End With
    End With
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Code:
Sub Test()

Dim lDstRowNum As Long
    With Sheets(1)
        lDstRowNum = .Cells(.Rows.Count, "A").End(xlUp).Row
        If lDstRowNum > 5 And IsEmpty(Rows(6)) = False Then
            With .Range("P6:P" & lDstRowNum)
                .Formula = "=MATCH(A6,rngSecSort,0)"
            End With
            .Range(gGroupHeader).CurrentRegion.Sort Key1:=.Range("P6"), Order1:=xlAscending, _
            Key2:=.Range("E6"), Order2:=xlAscending, Key3:=.Range("G6"), Order3:=xlDescending, Header:=xlGuess, _
            ordercustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
        Else: Exit Sub
        End If
        With .Range("P6:P" & lDstRowNum)
            .ClearContents
        End With
    End With
End Sub
See if this works for you. I made the assumption that if there was no data in row 6, then there isn't data to be sorted in rows 7-End.

A few questions/comments:

1) What do you mean when you say 'no data'? Will the cells be totally blank (absolutely nothing in them, including formulas that return "")? ...IsBlank(Rows(6)) looks to see if the entire row is devoid of any characters.

2) I used the entire Row(6) as the criteria. If you have a more definite range to substitute in, change rows(r).
 
Last edited:
Upvote 0
Thanks this works as required

Code:
Sub Test()
 
Dim lDstRowNum As Long
    With Sheets(1)
        lDstRowNum = .Cells(.Rows.Count, "A").End(xlUp).Row
        If lDstRowNum > 5 And IsEmpty(Rows(6)) = False Then
            With .Range("P6:P" & lDstRowNum)
                .Formula = "=MATCH(A6,rngSecSort,0)"
            End With
            .Range(gGroupHeader).CurrentRegion.Sort Key1:=.Range("P6"), Order1:=xlAscending, _
            Key2:=.Range("E6"), Order2:=xlAscending, Key3:=.Range("G6"), Order3:=xlDescending, Header:=xlGuess, _
            ordercustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
        Else: Exit Sub
        End If
        With .Range("P6:P" & lDstRowNum)
            .ClearContents
        End With
    End With
End Sub
See if this works for you. I made the assumption that if there was no data in row 6, then there isn't data to be sorted in rows 7-End.

A few questions/comments:

1) What do you mean when you say 'no data'? Will the cells be totally blank (absolutely nothing in them, including formulas that return "")? ...IsBlank(Rows(6)) looks to see if the entire row is devoid of any characters.

2) I used the entire Row(6) as the criteria. If you have a more definite range to substitute in, change rows(r).
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

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