Sort Table A-once new value has been added

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,668
Office Version
  1. 2007
Platform
  1. Windows
The code below works well & now to take it one step more i wish for Table to be sorted A-Z

VBA Code:
Private Sub AddKeyToTableList_Click()
  Dim tbl As ListObject
  Dim newRow As ListRow
  Dim f As Range
 
  Set tbl = Sheets("INFO").ListObjects("Table38")
 
  If TextBox3.Value = "" Then
    MsgBox "ENTER NEW KEY TYPE"
    Exit Sub
  End If
 
  Set f = tbl.Range.Find(TextBox3.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    MsgBox "THE KEY ALREADY EXISTS"
    Exit Sub
  End If
 
  Set newRow = tbl.ListRows.Add
 
  newRow.Range(1) = TextBox3.Value
  ComboBox1.RowSource = ""
  ComboBox1.Clear
  ComboBox1.List = tbl.Range.Value
End Sub

I added what i thought would work but i see a message about Invalid or Unqualified referance


Code:
Private Sub AddKeyToTableList_Click()
  Dim tbl As ListObject
  Dim newRow As ListRow
  Dim f As Range
 
  Set tbl = Sheets("INFO").ListObjects("Table38")
 
  If TextBox3.Value = "" Then
    MsgBox "ENTER NEW KEY TYPE"
    Exit Sub
  End If
 
  Set f = tbl.Range.Find(TextBox3.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    MsgBox "THE KEY ALREADY EXISTS"
    Exit Sub
  End If
 
  Set newRow = tbl.ListRows.Add
          .Sort.SortFields.Clear
        .Sort.SortFields.Add KEY:=.ListColumns(1).Range, SortOn:=xlSortOnValues, _
                                    Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    With .Sort
         .Header = xlYes
         .Apply

  newRow.Range(1) = TextBox3.Value
  ComboBox1.RowSource = ""
  ComboBox1.Clear
  ComboBox1.List = tbl.Range.Value
End Sub

Headers in Row 1
Values at present are DP2-DP12 & is Table38

Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Your code looks very incomplete eg you are adding a row at Set newRow but doing nothing with it.
You have seem to be missing With and End With statements.

If you focus on just the sort bit it should look something like this:
VBA Code:
    With tbl.Sort
        .SortFields.Clear
        .SortFields.Add Key:=tbl.ListColumns(1).Range, SortOn:=xlSortOnValues, _
                                    Order:=xlAscending, DataOption:=xlSortTextAsNumbers
        .Header = xlYes
        .Apply
    End With
 
Upvote 0
I have just noticed the newRow is being populated "after" the sort. Wouldn't you want to populate it "before" the sort ?
 
Upvote 0

Forum statistics

Threads
1,222,905
Messages
6,168,949
Members
452,227
Latest member
sam1121

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