+ Reply to Thread
Results 1 to 24 of 24

Help with a VBA Code (Dropdown list)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Help with a VBA Code (Dropdown list)

    Hello,

    The code in this example enable us to select multiple values from a dropdown list.

    However , for example, if I want to delete Q8- from the list in the 4th row and select another one I get an error message.

    Can anyone help with the VBA code?

    Thank you
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Help with a VBA Code (Dropdown list)

    Try something like this...

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldVal As String
    Dim newVal As String
    Dim i As Long
    Dim bRemoved As Boolean
    Dim v As Variant
        
    If Target.Count = 1 Then
        If Not Intersect(Target, Columns(2).SpecialCells(xlCellTypeAllValidation)) Is Nothing Then
            Application.EnableEvents = False
            On Error GoTo ExitHandler
            If Len(Target.Value) Then
                newVal = Target.Value
                Application.Undo
                oldVal = Target.Value
                If oldVal <> "" Then
                    v = Split(oldVal, ", ")
                    oldVal = ""
                    For i = LBound(v) To UBound(v)
                        If v(i) <> newVal Then
                            oldVal = oldVal & IIf(Len(oldVal), ", ", "") & v(i)
                        Else
                            bRemoved = True
                        End If
                    Next i
                    Target.Value = oldVal & IIf(bRemoved, "", ", " & newVal)
                Else
                    Target.Value = newVal
                End If
            End If
        End If
    End If
        
    ExitHandler:
      If Err.Number <> 0 Then MsgBox "Error " & Err.Number & vbLf & Err.Description, _
                              vbCritical, "ERROR: Worksheet_Change procedure": Err.Clear
      Application.EnableEvents = True
      
    End Sub

  3. #3
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Re: Help with a VBA Code (Dropdown list)

    Hello Alpha,

    First off all thank you for replying to this thread. I tried the code you gave. I am still not able to delete a value from the list and add new one without deleting the whole list.

    N.B: i removed the code i have and put yours.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Help with a VBA Code (Dropdown list)

    How are you trying to delete the value?

    If you re-select from the list a previously selected value, it is then removed. It seems to work for me in your example workbook.

  5. #5
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Re: Help with a VBA Code (Dropdown list)

    hmm...I see your point. Your code is a solution actually.

    What i wanted is to delete from a list a certain value and then selecting another one from the dropdown list.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Help with a VBA Code (Dropdown list)

    Quote Originally Posted by Naja View Post
    hmm...I see your point. Your code is a solution actually.

    What i wanted is to delete from a list a certain value and then selecting another one from the dropdown list.
    How did you want to delete? You would have to be more specific.

    Otherwise, mark this as solved.

  7. #7
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Re: Help with a VBA Code (Dropdown list)

    Hello,

    I entered your code, I am able to delete a value from the dropdown list BUT if I try to type anything in any cell, i get this error message :
    ''Error 9 Subscript out of range''.

    Can you fix it?

    Thank you

    Here is the file with the code you gave me Sample N with new code.xlsm

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Help with a VBA Code (Dropdown list)

    I cannot replicate the error with the file you provided. In several cells I typed, deleted, copied. Everything I tried produced no errors.

    Does the error message have the title "ERROR: Worksheet_Change procedure"?

    Does the error message have a Debug button? If yes, is a line of code highlighted when you click it?

    Also, no need to PM me. I get notified when you reply to this thread.

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Help with a VBA Code (Dropdown list)

    To help diagnose, comment out the error handler code (below). Then does the error message now have a debug button and if yes, what line is highlighted.

    Note: you will have to re-enable Application.Events after the error.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldVal As String
    Dim newVal As String
    Dim i As Long
    Dim bRemoved As Boolean
    Dim v As Variant
    
    If Target.Count = 1 Then
        If Not Intersect(Target, Columns(2).SpecialCells(xlCellTypeAllValidation)) Is Nothing Then
            Application.EnableEvents = False
    '        On Error GoTo ExitHandler
            If Len(Target.Value) Then
                newVal = Target.Value
                Application.Undo
                oldVal = Target.Value
                If oldVal <> "" Then
                    v = Split(oldVal, ", ")
                    oldVal = ""
                    For i = LBound(v) To UBound(v)
                        If v(i) <> newVal Then
                            oldVal = oldVal & IIf(Len(oldVal), ", ", "") & v(i)
                        Else
                            bRemoved = True
                        End If
                    Next i
                    Target.Value = oldVal & IIf(bRemoved, "", ", " & newVal)
                Else
                    Target.Value = newVal
                End If
            End If
        End If
    End If
        
    ExitHandler:
    '  If Err.Number <> 0 Then MsgBox "Error " & Err.Number & vbLf & Err.Description, _
    '                          vbCritical, "ERROR: Worksheet_Change procedure": Err.Clear
    '  Application.EnableEvents = True
      
    End Sub

  10. #10
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Re: Help with a VBA Code (Dropdown list)

    I did that. Now i cannot choose multiple values from the dropdown list...

    Here is the new file with the your new code

    Sample N with new code3.xlsm

  11. #11
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Help with a VBA Code (Dropdown list)

    You only pasted part of the code to the new file.

    All this is missing...
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oldVal As String
    Dim newVal As String
    Dim i As Long
    Dim bRemoved As Boolean
    Dim v As Variant

  12. #12
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Re: Help with a VBA Code (Dropdown list)

    Done Sample N with new code4.xlsm

    I still cannot choose mutliple values... and no error message

  13. #13
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Help with a VBA Code (Dropdown list)

    Note: you will have to re-enable Application.Events.

    Put this code in a standard code module (e.g.; Module1) and run it.

    Sub ReEnable_Events()
        Application.EnableEvents = True
    End Sub

  14. #14
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Re: Help with a VBA Code (Dropdown list)

    I am a little confused..
    But i think i did that. Have a look Sample N with new code4.xlsm

    I got nothing.

    N.B: thank you so much for trying to figure this out with me

  15. #15
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Re: Help with a VBA Code (Dropdown list)

    What if we try in the code make a direct reference to the activie sheet or something like that?

  16. #16
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Help with a VBA Code (Dropdown list)

    Quote Originally Posted by Naja View Post
    What if we try in the code make a direct reference to the activie sheet or something like that?

    What if we try to make a reference to the activite sheet?
    Not needed and also not needed the 2nd time and for future reference it wont be needed.

    Close Excel completely and restart it and open only the last version of the workbook (code4).

  17. #17
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Re: Help with a VBA Code (Dropdown list)

    What if we try to make a reference to the activite sheet?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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