Macro to select column to run 'sort by colours' macro on

Sallyprit

New Member
Joined
Jan 25, 2024
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hope one of you excel gurus can assist? 😊 I have recorded the following macro to sort column AE2 by cell colour. I now wish to be able to select any column to run the macro on, please can you advise? Many thanks indeed in advance

Sub SortingByColour()
'
' sortingbycolour Macro
' sorting for insights due within a month to due within 120 days
'

'
ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Add( _
Range("AE2"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
color = RGB(255, 192, 0)
ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Add( _
Range("AE2"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
color = RGB(255, 255, 0)
ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Add( _
Range("AE2"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
color = RGB(146, 208, 80)
ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Add( _
Range("AE2"), xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
color = RGB(253, 233, 217)
With ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply

End With
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
If you select the cell you want to use before running the code, I think this modification to your original code should work:
VBA Code:
Sub SortingByColour()
'
' sortingbycolour Macro
' sorting for insights due within a month to due within 120 days
'

'   Capture active cell to sort on
    Dim rng As Range
    Set rng = ActiveCell
    
'
    ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Clear
    
    ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Add( _
        rng, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
        Color = RGB(255, 192, 0)
    
    ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Add( _
        rng, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
        Color = RGB(255, 255, 0)
    
    ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Add( _
        rng, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
        Color = RGB(146, 208, 80)
    
    ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort.SortFields.Add( _
        rng, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue. _
        Color = RGB(253, 233, 217)
    
    With ActiveWorkbook.Worksheets("120 day insight").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
End Sub
 
Upvote 1
Solution
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,258
Members
449,307
Latest member
Andile

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