Results 1 to 11 of 11

Sorted Data Validation List on Another Sheet code very wordy.

Threaded View

  1. #1
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Question Sorted Data Validation List on Another Sheet code very wordy.

    Hello!

    I have a clunky code which takes the value in cell C5 on SHEETB and uses it for a dropdown validation list from a named range on SHEETA.
    If the C5 value is not on the dropdown, it asks you if you want to add it, so the next time you use that worksheet, you won't have to re-type that new value.

    So as new values get added, the dropdown list grows and it does a sort so that the list is always in alphabetical order.

    I'd appreciate it if someone can look at this code. I used the macro recorder alot, and this result works but it seems to me like some of the steps can be combined. And then I set the range to 10,000 entries so I'm not sure what's going to happen if there's 10,001 (will that one not get sorted?), etc.

    Regards,
    Leaning




    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lReply As Long
    On Error GoTo OrderlyExit
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Address = "$C$5" Then
    If IsEmpty(Target) Then Exit Sub
    Application.EnableEvents = False
    If WorksheetFunction.CountIf(ThisWorkbook.Names("UnitInfo").RefersToRange, Target) = 0 Then
    lReply = MsgBox("Add " & Target & " to list? (Note: Once it's there, you're stuck with it.)", vbYesNo + vbQuestion)
    If lReply = vbYes Then
    'Sheets("SheetA").Unprotect Password:="unprotect"
    With ThisWorkbook.Names("UnitInfo").RefersToRange.Cells(ThisWorkbook.Names("UnitInfo").RefersToRange.Rows.Count, 1)
    If Len(.Value) > 0 Then
    .Offset(1, 0).Value = Target
    Else
    .Value = Target
    End If
    MsgBox (Target & " added to list.")
    End With
    'Sheets("SheetA").Protect Password:="protect"
    End If
    End If
    Application.EnableEvents = True
    End If
    ActiveWorkbook.Sheets("SHEETA").Select
        ActiveWorkbook.Sheets("SHEETA").Columns("D:D").Select
        ActiveWorkbook.Worksheets("SHEETA").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("SHEETA").Sort.SortFields.Add Key:=Range("D1"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("SHEETA").Sort
            .SetRange Range("D1:D10000")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
      
    End With
    Sheets("SHEETB").Select
    Range("C5").Select
    OrderlyExit: Application.EnableEvents = True
    End Sub

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Sorted and unique validation list
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-23-2016, 05:20 PM
  2. [SOLVED] Extract a non-sorted list with blank cells into a sorted list
    By RJK in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-08-2015, 12:41 AM
  3. Sorted data in drop list (data validation)
    By ONB in forum Excel General
    Replies: 4
    Last Post: 08-30-2012, 07:22 AM
  4. Replies: 11
    Last Post: 10-06-2011, 02:26 PM
  5. Replies: 7
    Last Post: 11-11-2010, 09:51 PM
  6. Range is only one cell. Less wordy code?
    By leaning in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2010, 08:00 AM
  7. Need sorted validation list
    By [email protected] in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-23-2005, 02:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1