Sorting when a column or 2 is empty

SandsB

Well-known Member
Joined
Feb 13, 2007
Messages
705
Office Version
  1. 365
Platform
  1. Windows
This code sorts my data based on the value in column B. But - one (or more) of the columns in my data is frequently empty and then the columns to the right of it aren't sorted. What changes could I make to ensure sorting by column B doesn't leave some columns unsorted?


VBA Code:
 Sheets("Pending").Select
    Range("B2").Select
    ActiveWorkbook.Worksheets("Pending").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Pending").Sort.SortFields.Add2 Key:=Range( _
        "B2:B10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Pending").Sort
        .SetRange Range("B2:B10000")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Last edited by a moderator:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try this:
Change the letter "F" in the following code to the letter in the right column that you need to be included in the sort.

Rich (BB code):
Sub sortingdata()
  Sheets("Pending").Select
  Range("B2:F" & Range("B" & Rows.Count).End(3).Row).Sort Range("B2"), xlAscending, Header:=xlNo
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,096
Latest member
Anshu121

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