VBA Compile Error: Named argument not found

thomji1

New Member
Joined
Feb 13, 2025
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am trying to highlight cells in an individual column, and it errors on the Color:=RGB(255, 255, 0 portion of this code:

' Sort column A by color (highlighted cells at the top)
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=ws.Range("A1:A" & lastRow), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:=xlSortNormal, Color:=RGB(255, 255, 0)
With ws.Sort
.SetRange ws.Range("A1:I" & lastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Anyone have suggestions on how to fix Color:= ???
 
Hello,

It is because SortFields.Add method (Excel) | Microsoft Learn does not have a "color" argument, in red on your code.

Maybe you were looking for the Range.AutoFilter method (Excel) | Microsoft Learn method, which allows you to filter on colors. Otherwise, just remove it, sorting is not filtering, you do not need to provide a color.
VBA Code:
ws.Sort.SortFields.Add Key:=ws.Range("A1:A" & lastRow), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:=xlSortNormal
 
Upvote 0
Solution
Hello,

It is because SortFields.Add method (Excel) | Microsoft Learn does not have a "color" argument, in red on your code.

Maybe you were looking for the Range.AutoFilter method (Excel) | Microsoft Learn method, which allows you to filter on colors. Otherwise, just remove it, sorting is not filtering, you do not need to provide a color.
VBA Code:
ws.Sort.SortFields.Add Key:=ws.Range("A1:A" & lastRow), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:=xlSortNormal
OK, I kind of see where you are going with this. So what part of that line should I remove or change to fix it?
 
Upvote 0

Forum statistics

Threads
1,226,886
Messages
6,193,511
Members
453,804
Latest member
Daniel OFlanagan

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