sorting in alphabetical order not working when a combo box is linked to a named range

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,392
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a spreadsheet that works fine but I have just been told the combo box that pulls a named range from another sheet needs to sort in alphabetical order.

I run this procedure to add a young person
VBA Code:
Sub AddYP()
Application.DisplayAlerts = False
Dim newyp As String
    'Assign the name in D5 of the Tracker sheet to the variable newyp
    newyp = Tracker.Cells(5, 4)
    'Offset from the bottom of the list of YP by 1 to add the newyp to the list
    YP.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = newyp
        'Call CreateWB sub and send the variable newyp to it
        Call CreateWB(newyp)
    'Define a new named range for tblYPNames by resizing the current range by _
    one row down
    ThisWorkbook.Names.Add Name:="tblYPNames", _
    RefersTo:=Range("tblYPNames").Resize(Range("tblYPNames").Rows.Count + 1)
        Tracker.cboYP.ListFillRange = "tblYPNames"
    'Sort the YP names list in alphabetical order
    Call Sort
Application.DisplayAlerts = True
End Sub

It works fine until it gets down to the sort sub

I get the error method range of object worksheet failed with this line highlighted
VBA Code:
Sub Sort()
YP.Range("A2", Range("A2").End(xlUp)).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo
End Sub

The combo box has a listfillrange of tblYPNames.

Can someone help me please?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try using
VBA Code:
YP.Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo
 
Upvote 0
Thanks for the reply Michael. That works fine but I want it to sort the names on the sheet with the code name YP.

I enter YP before range and I get the error, the sort reference is not valid.

It highlights
VBA Code:
YP.Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo
 
Upvote 0
You need to add YP in front the 2 other places it says Range in the code or else it will error if YP is not the ActiveSheet.
 
Upvote 0
As @MARK858 stated, if the YP sheet isn't the active sheet you will need
VBA Code:
YP.Range("A2:A" & YP.Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=YP.Range("A2"), Order1:=xlAscending, Header:=xlNo
 
Upvote 0
Thanks guys, totally missed that.

This appears to be working, is this the correct code with a header in A1?
VBA Code:
YP.Range("A1:A" & YP.Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=YP.Range("A1"), Order1:=xlAscending, Header:=xlYes
 
Upvote 0
Yes, I did. It appears to be working. I guess if it is working, it is working.

Thanks guys for your help.
 
Upvote 0

Forum statistics

Threads
1,226,112
Messages
6,189,041
Members
453,521
Latest member
Chris_Hed

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