+ Reply to Thread
Results 1 to 11 of 11

Sorted Data Validation List on Another Sheet code very wordy.

Hybrid 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

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Sorted Data Validation List on Another Sheet code very wordy.

    I would do it differently, but obviously the choice is yours.
    In data validation I would have this.
    Source:
    =SHEETA!$D:$D

    Then I would have the following code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    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
            Dim x As Boolean
            x = WorksheetFunction.CountIf(Worksheets("SHEETA").Range("D:D"), Target.Value)
            If Not x Then
                lReply = MsgBox("Add " & Target & " to list? (Note: Once it's there, you're stuck with it.)", vbYesNo + vbQuestion)
                    If lReply = vbYes Then
                        With Worksheets("SHEETA")
                            .Cells(Rows.Count, "D").End(xlUp)(2).Value = Target.Value
                            .Columns("D").Sort Key1:=.Range("D1"), Order1:=xlAscending, Header:=xlNo
                            MsgBox (Target & " added to list.")
                        End With
                    End If
            End If
    End If
    Application.EnableEvents = True
    'Sheets("SheetA").Unprotect Password:="unprotect"
    
    'Sheets("SheetA").Protect Password:="protect"
    
    Exit Sub
    OrderlyExit: Application.EnableEvents = True
    End Sub
    Last edited by skywriter; 01-26-2016 at 10:32 PM.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

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

    Re: Sorted Data Validation List on Another Sheet code very wordy.

    Skywriter: That's awesome! It works tons different and smoother without the screen flickering all over the place from changing sheets.
    I appreciate your help. I think you chopped almost half the lines of code, so yeah, it needed some work. Mad props!

    Regards,
    Leaning

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

    Re: Sorted Data Validation List on Another Sheet code very wordy.

    Skywriter,

    I took the example workbook you fixed and tried to apply it to the actual workbook. It works great, but the dropdown in the cell has a bunch of extra white space in it that I can't tell where it's coming from. Any ideas? All PWs are "help", BTW.
    Regards,
    Leaning
    Attached Files Attached Files
    Last edited by leaning; 01-27-2016 at 12:06 AM.

  5. #5
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Sorted Data Validation List on Another Sheet code very wordy.

    Your project is locked, I can't see the code.

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

    Re: Sorted Data Validation List on Another Sheet code very wordy.

    skywriter: the protect, unprotect, and VBA passwords are all "help". My actual project is locked so I wanted to see if maybe the lock was causing problems and creating that white space.

    HTH.
    Regards,
    Leaning

  7. #7
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Sorted Data Validation List on Another Sheet code very wordy.

    Your location is spelled wrong, you are in Michigan?

    If you are happy with my help a bump of the reputation button would be appreciated.
    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$20" Then
        If IsEmpty(Target) Then Exit Sub
            ThisWorkbook.Sheets("My MEPS").Unprotect Password:="help"
            Application.EnableEvents = False
            Dim x As Boolean
            x = WorksheetFunction.CountIf(Worksheets("My MEPS").Range("L:L"), Target.Value)
            If Not x Then
                lreply = MsgBox("Add " & Target & " to list? (Note: Once it's there, you're stuck with it.)", vbYesNo + vbQuestion)
                    If lreply = vbYes Then
                        With Worksheets("My MEPS")
                            .Cells(Rows.Count, "L").End(xlUp)(2).Value = Target.Value
                            .Columns("L").Sort Key1:=.Range("L1"), Order1:=xlAscending, Header:=xlNo
                            MsgBox (Target & " added to list.")
                            Target.Validation.Modify Formula1:="='My MEPS'!$L$1:$L$" & .Cells(Rows.Count, "L").End(xlUp).Row
                        End With
                    End If
            End If
    End If
    Application.EnableEvents = True
    ThisWorkbook.Sheets("My MEPS").Protect Password:="help"
    Exit Sub
    OrderlyExit: Application.EnableEvents = True
    End Sub
    Last edited by skywriter; 01-27-2016 at 01:36 AM.

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

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

    skywriter: still not working on my end. Dropdown still has extra spaces. Any other ideas? I highlighted all the data cells purple, but it doesn't look like there is anything below that is making the dropdown list so long. I'm stumped. Thanks for sticking with me through this. And I updated my location. Thanks for that!

    Regards,
    Leaning
    Attached Files Attached Files

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

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

    Duplicate post
    Last edited by leaning; 01-27-2016 at 10:20 AM.

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

    Re: Sorted Data Validation List on Another Sheet code very wordy.

    I kept getting blank lines in the dropdown using skywriter's code, so I'm back to my clunky code:

    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$20" 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("MyM").Unprotect Password:="help"
    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
    End If
    End If
    Application.EnableEvents = True
    End If
    ActiveWorkbook.Sheets("MyM").Select
        ActiveWorkbook.Worksheets("MyM").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("MyM").Sort.SortFields.Add Key:=Range("L1"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("MyM").Sort
            .SetRange Range("L1:L10000")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
      
    End With
    Sheets("MyM").Protect Password:="help"
    Sheets("17").Select
    Range("C20").Select
    OrderlyExit: Application.EnableEvents = True
    End Sub
    If anyone has any ideas on making this better and less flickering, I'd appreciate it.

    Regards,
    Leaning

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

    Re: Sorted Data Validation List on Another Sheet code very wordy.

    This is better:

    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$20" 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("MyM").Unprotect Password:="help"
    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
    End If
    End If
    With Sheets("MyM")
    .Range("L1:L1000000").Sort Key1:=.Range("L1"), Order1:=xlAscending, Header:=xlNo
    End With
    Sheets("MyM").Protect Password:="help"
    Application.Goto (ThisWorkbook.Sheets("17").Range("C20"))
    Application.EnableEvents = True
    End If
    OrderlyExit: Application.EnableEvents = True
    End Sub
    Last edited by leaning; 01-28-2016 at 11:02 PM.

+ Reply to Thread

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