Solved

Excel VBA - Sort multiple tables when new items added

Posted on 2015-01-07
3
319 Views
Last Modified: 2015-01-07
Hi Experts,

I have a sheet of single column tables that are ultimately used in data validation on another sheet in the workbook.  I'd like help with adding code to the workbook which would sort any/all of the tables if the user adds a new entry.  Workbook attached.

Appreciate any help you can offer!  Thanks!
EE-TableSortTest.xlsm
0
Comment
Question by:xllvr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40536538
Add this code to the LISTS sheet.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim lo As ListObject

For Each lo In Sheets("Lists").ListObjects
    SortTable lo
Next

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub
Private Sub SortTable(lo As ListObject)
   ActiveWorkbook.Worksheets("LISTS").ListObjects(lo.Name).Sort.SortFields _
        .Clear
    ActiveWorkbook.Worksheets("LISTS").ListObjects(lo.Name).Sort.SortFields _
        .Add Key:=Range(GetRange(lo.Range.Columns(1).Column)), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("LISTS").ListObjects(lo.Name).Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
Private Function GetRange(col As Integer) As String
    GetRange = Split(Cells(1, col).Address, "$")(1) & ":" & Split(Cells(1, col).Address, "$")(1)
End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:xllvr
ID: 40536571
This is excellent!  Thanks so much, Martin!
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40536611
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question