.AdvancedFilter (Alphjabetical ordering?)

Dancey_

New Member
Joined
Aug 18, 2010
Messages
41
Hey guys,

I am using the below to copy a row of data (Staff Names) across to a sheet so I can use the data in a drop down selection list that will be used to generate a sales report.

Does anybody know how to leave the source data as is but make the copied data alphabetical order?

My code is:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Set shtReport = Sheets("Reporting")
    Columns("C:C").Select
    Columns("C:C").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=shtReport.Range("S:S"), Unique:=True
End Sub

Thanks!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Thanks for the idea but I need to be able to do this automatically.

The data will be used as a source for a data validation in cell drop down list.

Cheers
 
Upvote 0
I meant that you should sort it in the macro. Like:
Code:
shtReport.Range("S1", shtReport.Range("S65536").End(xlUp)).Sort _
    Key1:=Range("S1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
 
Upvote 0
Thanks for the code dude. I have changed my sub slightly so it now runs on the press of a button, I added your code to the bottom, but it doesn't work. I get 400 in a pop up, the first part of the code is working as it is copying my list, but it isn't sorting it.

No doubt I have done something stupid, can you please help?

Code:
Sub GenerateUsers()
Set sht1 = Sheets("Sales by Agent")
Sheets("Sales").Activate
    Columns("C:C").Select
    Columns("C:C").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=sht1.Range("S:S"), Unique:=True
    sht1.Range("S1", sht1.Range("S65536").End(xlUp)).Sort _
    Key1:=Range("S1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
End Sub
 
Upvote 0
I tried that before, I have tried this:

Code:
Sub GenerateUsers()
Set sht1 = Sheets("Sales by Agent")
Sheets("Sales").Activate
    Columns("C:C").Select
    Columns("C:C").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=sht1.Range("S:S"), Unique:=True
sht1.Activate
    sht1.Range("S1", sht1.Range("S65536").End(xlUp)).Sort _
    Key1:=Range("S1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
End Sub

and this:

Code:
Sub GenerateUsers()
Set sht1 = Sheets("Sales by Agent")
Sheets("Sales").Activate
    Columns("C:C").Select
    Columns("C:C").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=sht1.Range("S:S"), Unique:=True
Sheets("Sales by Agent").Activate
    sht1.Range("S1", sht1.Range("S65536").End(xlUp)).Sort _
    Key1:=Range("S1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
End Sub

Neither seems to work.

Thanks for the help :)
 
Upvote 0
What code do you get if you macro record yourself sorting the range? That may be a better starting place than my code.
 
Upvote 0
I get the following code:

Code:
Sub SortAZ()
'
' SortAZ Macro
'

'
    Columns("S:S").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Sales by Agent").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sales by Agent").AutoFilter.Sort.SortFields.Add Key _
        :=Range("S1:S9"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Sales by Agent").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Which I dropped in with mine:

Code:
Sub GenerateUsers()
Set sht1 = Sheets("Sales by Agent")
Sheets("Sales").Activate
    Columns("C:C").Select
    Columns("C:C").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=sht1.Range("S:S"), Unique:=True
Sheets("Sales by Agent").Activate
 Columns("S:S").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Sales by Agent").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sales by Agent").AutoFilter.Sort.SortFields.Add Key _
        :=Range("S1:S9"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Sales by Agent").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Which gave the same error.

So, I did this:

Code:
Sub GenerateUsers()
Set sht1 = Sheets("Sales by Agent")
Sheets("Sales").Activate
    Columns("C:C").Select
    Columns("C:C").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=sht1.Range("S:S"), Unique:=True
Call SortAZ
End Sub

Which runs this when it is done:

Code:
Sub SortAZ()
    Sheets("Sales by Agent").Activate
    Columns("S:S").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Sales by Agent").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sales by Agent").AutoFilter.Sort.SortFields.Add Key _
        :=Range("S1:S9"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Sales by Agent").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

AND IT WORKS :biggrin: Thanks for your help!!
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,283
Members
452,902
Latest member
Knuddeluff

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