Drop Down List - Add New Items

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
585
Office Version
  1. 365
Platform
  1. Windows
I'm using Debra Dalgleish's "Drop Down List - Add New Items" code in the link below to allow users of the worksheet the opportunity to add new items to their drop down list if they want to. I use a ComboBox in my Userform to chose from the existing drop down list. However, the Userform won't allow adding new items to the drop down list like the user can currently do when in the worksheet. How could I get the ComboBox of the Userform trigger Debra Dalgleish's code to allow my user to add a new item to the Drop Down list from within the Userform? Any ideas or suggestions would be greatly appreciated. This is the last thing I need to do for this project I'm trying to complete. The "cmdAddUpdatePOData_Click" code I have is in another thread. I can add it here if that helps. Thanks, SS

Excel Drop Down Lists - Add New Items
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hello! I would add a button to your userform, and then run this code when the button is pressed:
VBA Code:
Private Sub CommandButton1_Click() 'If you change the name of your button in the user form (properties pane), also change it here.
Dim NewItem As String

NewItem = InputBox("Please choose a new value to add to the dropdown") 'You can edit the prompt to whatever you'd like

Me.ComboBox1.AddItem NewItem 'Change "Combobox1" to the name of your combobox in the userform

End Sub
 
Upvote 0
use ComboBox_BeforeUpdate
That is what I tried, but got a Method of "Activate method of 'Range' class failed" error in my code code below:

Error on this line:
VBA Code:
            POnum.Offset(, 1).Activate

VBA Code:
Private Sub ComboBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

    Dim ws As Worksheet
    Dim tb As ListObject
    Dim PO_No As String, POnum As Range

    Set ws = ThisWorkbook.Sheets("2022")
    Set tb = ws.ListObjects("Table46")

    PO_No = Trim(TextBox1.Text)
    
    With tb.Range.Columns(4)

        Set POnum = .Find(What:=PO_No, after:=.Cells(1), LookIn:=xlValues, _
                          SearchOrder:=xlByRows, searchdirection:=xlNext)

        If Not POnum Is Nothing Then

            TextBox1 = POnum

            POnum.Offset(, 1) = ComboBox2.Text
            
            POnum.Offset(, 1).Activate

        End If

    End With

End Sub
 
Upvote 0
How is your ComboBox getting its value? If you are using the .RowSource property, then the .AddItem method won't work.
Better than .RowSource would be to use the .listproperty to fill the list.

VBA Code:
ComboBox1.List = Application.Transpose(Range("A1:A20").Value)
 
Upvote 0
Solution
I suspect Sheets("2022") is not the active sheet

The Debra Dalgleish thing adds a new item to the bottom of a named range.
Doesn't appear to be what you're trying to accomplish.
 
Upvote 0
I suspect Sheets("2022") is not the active sheet

The Debra Dalgleish thing adds a new item to the bottom of a named range.
Doesn't appear to be what you're trying to accomplish.
I am in the process of updating all of my code to call the sheets the Active Sheets as well as reference the table as the only table on each worksheet (below is what I'm using for that).

VBA Code:
    Dim ws As Worksheet
    Dim tb As ListObject
    Dim PO_No As String, POnum As Range
    
    Set ws = ThisWorkbook.ActiveSheet 'Sheets("2022")
'    assumes Table is the first one on the ActiveSheet
    Set tb = ActiveSheet.ListObjects(1)
'    Set tb = ws.ListObjects("Table46")

I actually do want to accommodate the user by letting them add items to the list with a message box that ask them first if they want to add the item to the list. It works on the worksheet drop-downs, but not on the UserForm, which is what her code does.

Below is what my "cmdAddUpdatePOData_Click" code looks like right now.

VBA Code:
Private Sub cmdAddUpdatePOData_Click()

    Dim ws As Worksheet
    Dim tb As ListObject
    Dim PO_No As String, POnum As Range
    
    Set ws = ThisWorkbook.ActiveSheet 'Sheets("2022")
'    assumes Table is the first one on the ActiveSheet
    Set tb = ActiveSheet.ListObjects(1)
'    Set tb = ws.ListObjects("Table46")

'PO_No = Trim(TextBox1.Text)
PO_No = Split(Trim(TextBox1.Text) & " ", " ")(0)    'Added per MrExcel message board, SPS, 09/29/22

    With Application
        .DisplayAlerts = False                'Turns off alerts
    '    .AlertBeforeOverwriting = False       'Turns off overwrite alerts
        .ScreenUpdating = False               'Turns off screen updating
    End With
   
    ShowHideLogSheet
    
    With tb.Range.Columns(4)
    
        Set POnum = .Find(What:=PO_No, after:=.Cells(1), LookIn:=xlValues, _
                          SearchOrder:=xlByRows, searchdirection:=xlNext)           '& "*" took out because code is adding a row now
                         
        If Not POnum Is Nothing Then
            
            POnum = TextBox1.Text
                        
            POnum.Offset(, 1) = ComboBox2.Text
            POnum.Offset(, 2) = TextBox3.Text
            POnum.Offset(, 3) = ComboBox4.Text
            POnum.Offset(, 4) = ComboBox5.Text
            POnum.Offset(, 5) = ComboBox6.Text
            POnum.Offset(, 6) = TextBox7.Text
            POnum.Offset(, 7) = TextBox8.Text
            POnum.Offset(, 8) = ComboBox9.Text
            POnum.Offset(, 9) = TextBox10.Text
            POnum.Offset(, 10) = TextBox11.Text
            POnum.Offset(, 11) = TextBox12.Text

        Else
  
            If TextBox1.Text = "" Then
        
                cmdFindPO_Click
    
            Else
  
            MsgBox "Sorry, your PO_No was not found"
            
            End If
            
        End If
    
    End With

TextBox1.Text = Format(TextBox1.Text, "0000")

Repaint

    With Application
        .DisplayAlerts = True                'Turns on alerts
    '    .AlertBeforeOverwriting = False       'Turns off overwrite alerts
        .ScreenUpdating = True               'Turns on screen updating
    End With

End Sub
 
Last edited:
Upvote 0
Any reason for not answering post #5 ?

Knowing how, when and from where the combobox1 drop down is being populated is key to the solution you need.
 
Upvote 0
Any reason for not answering post #5 ?

Knowing how, when and from where the combobox1 drop down is being populated is key to the solution you need.
I'm trying to make that work right now. I did have a row source property in the properties box for that combobox.
 
Upvote 0
How is your ComboBox getting its value? If you are using the .RowSource property, then the .AddItem method won't work.
Better than .RowSource would be to use the .listproperty to fill the list.

VBA Code:
ComboBox1.List = Application.Transpose(Range("A1:A20").Value)
I'm sorry, but I cannot figure out where to try this line of code. The drop down table that I reference is on a sheet called "Drops" and in a named range called "tblSupplier".
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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