Sorting a range alphabetically after input box adds new value

ISqueal

New Member
Joined
Aug 12, 2017
Messages
6
I'm creating a worksheet that has one column and one header for the column. There is a button that prompts an input box and creates a new row in the column for the inputted value. I am trying to create a procedure that sorts the column alphabetically after the input box value has been added as a new row however I can't seem to get it to work.

This is my current code:

Dim NewOwner As Variant


NewOwner = InputBox("Enter Owner Name", "Add New Owner")

Dim ownersht As Worksheet
Dim ownerLastRow As Long

Set ownersht = ThisWorkbook.Worksheets("Owners")
Set ownerLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

Dim ownerRange As Range
Dim ownerkeycell As Range


Set ownerRange = Range(Cells(2, 1), Cells(ownerLastRow + 1, 1))
Set ownerkeycell = Cells(2, 1)


If NewOwner = vbOK Then
With ownersht
ownerRange.Sort Key1:=ownerkeycell, Order1:=xlAscending, Header:=xlYes
End With
End If

There is no error notification however the code just doesn't seem to work. Any help would be appreciated, thanks!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You have a few syntax errors. Untested, but see if this works:
Rich (BB code):
Sub AddNewOwner()
Dim NewOwner As Variant
Dim ownersht As Worksheet
Dim ownerLastRow As Long
Dim ownerRange As Range
Dim ownerkeycell As Range

NewOwner = InputBox("Enter Owner Name", "Add New Owner")
If NewOwner = "" Then Exit Sub  'user clicked cancel
Set ownersht = ThisWorkbook.Worksheets("Owners")
ownerLastRow = ownersht.Cells(ownersht.Rows.Count, "A").End(xlUp).Row
Set ownerRange = Range(Cells(2, 1), Cells(ownerLastRow + 1, 1))
Set ownerkeycell = Cells(2, 1)
With ownersht
    ownerRange.Sort Key1:=ownerkeycell, Order1:=xlAscending, Header:=xlYes
End With
End Sub
This assumes your header is in A2. If its actually in A1 then change ownerrange to start at Cells(1,1).
 
Upvote 0
Thank for the help! I didn't notice the Header issue and the exclusion of the If Then statement for the vbOk value made it work. Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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