VBA to sort by color - need a quick tweak

Mr Retirement

New Member
Joined
Nov 12, 2016
Messages
46
Hi All,

I am working through this code to sort my data by color ... then delete the rows of that color.

The below code works to sort but my table starts in row 5 and the code overrides it and starts the sort in row 2.

Can anyone help adjust this code or have a better way to approach this?

Code:
   Dim wks As Worksheet
   
  Set wks = Worksheets("Input")

   With wks.Sort
      With .SortFields
         .Clear
         .Add(wks.Range("B5"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(0, 0, 255)
         .Add Key:=wks.Range("A5"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
      End With
      .SetRange wks.Range("B5", Range("B" & Rows.Count).End(xlUp)).CurrentRegion
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With

Thanks,
Mr R
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
my table starts in row 5 and the code overrides it and starts the sort in row 2

At a glance, remove .CurrentRegion.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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