+ Reply to Thread
Results 1 to 14 of 14

Removing the Selected Item in a ComboBox

  1. #1
    Registered User
    Join Date
    03-04-2014
    Location
    Limerick, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    6

    Question Removing the Selected Item in a ComboBox

    Hello,
    Im using dependent Combo Boxes in an Excel File. Im trying to figure out how to remove the selected item in one of the Combo Boxes.
    Im using the .AddItem Function in the VBA Code to populate the Combo Boxes.
    Ive tried .... comboBox1.Items.Remove(comboBox1.SelectedItem) .... but no luck.

    I would be grateful for your help. Many Thanks

    PT

  2. #2
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Removing the Selected Item in a ComboBox

    Hello. An important question:

    - How did you fill the comboBox?... With the powerful RowSource or with the inefficient AddItem?
    You are always very welcome if you add reputation by clicking the * (bottom left) of each message that has helped you.

  3. #3
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Removing the Selected Item in a ComboBox

    If it was the second way I asked you for, then you can try:

    PHP Code: 
    Private Sub CommandButton1_Click()
    Dim i&
    With ComboBox1
      i 
    = .ListIndex
      
    If >= 0 Then .ListIndex = -1: .RemoveItem i
    End With
    End Sub 

  4. #4
    Registered User
    Join Date
    03-04-2014
    Location
    Limerick, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Removing the Selected Item in a ComboBox

    Hello,

    I am filling the Combo Box with the AddItem function.
    Unfortunately your code does not work.

  5. #5
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: Removing the Selected Item in a ComboBox

    do you want a permanent removal or only removed for the duration of your app.
    upload your workbook for a concise solution.
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  6. #6
    Registered User
    Join Date
    03-04-2014
    Location
    Limerick, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    6

    Question Re: Removing the Selected Item in a ComboBox

    Hello,

    Yes, I want to permanently remove the item in the Food Item Combo Box.

    This is what i wish to achieve:

    1. Open the User Form
    2. Click on the Food Type Combo Box and Select a Food Type, e.g Grains
    3. This adds the items - Cereals, Bread, Pasta to the Food Item Combo Box
    4. When I select Cereals it writes this to Cell C5.
    (All of the above is working OK)

    Now ... this is what I wish to do beyond this point

    5. Remove Grains from the Food Item Combo Box when I return to it after selecting one of the other Food Types in the Food Type Combo Box.

    Hope this makes sense and that the attachment has been included in this message!

    Thank you.Example_Two Dependent Combo Boxes on UserForm.xlsm

  7. #7
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Removing the Selected Item in a ComboBox

    Quote Originally Posted by MacroPete View Post
    Unfortunately your code does not work.
    The suggested works perfectly.

    What happens is that when you implement what is suggested, a recursive process starts that ends up giving you an error.

    In fact: it would seem that what you should delete is the chosen element from ComboBox2 and not from ComboBox1: Do you want to think better?...

  8. #8
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Removing the Selected Item in a ComboBox

    Analyze if the attached model is more like what you want to achieve:
    Attached Files Attached Files
    Last edited by beyond Excel; 03-30-2023 at 12:04 PM.

  9. #9
    Registered User
    Join Date
    03-04-2014
    Location
    Limerick, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Removing the Selected Item in a ComboBox

    Hello,

    Yes indeed ... this works very well! Thank you.
    I cant access the UserForm Code though. Could you send me a version of this file so that i can edit the code further?

    Many thanks.

  10. #10
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Removing the Selected Item in a ComboBox

    Quote Originally Posted by MacroPete View Post
    I cant access the UserForm Code though.
    a) Do not forget to 'Click' on the * found in the lower-left part of post #8 to thank the proposal.

    b) What do you do to enter the VBA environment?...

  11. #11
    Registered User
    Join Date
    03-04-2014
    Location
    Limerick, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Removing the Selected Item in a ComboBox

    Hello beyond Excel,

    Yes I have accessed the code now. It is all ok. Many thanks again for your help.

  12. #12
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Removing the Selected Item in a ComboBox

    Quote Originally Posted by MacroPete View Post
    Hello beyond Excel,

    Yes I have accessed the code now. It is all ok. Many thanks again for your help.
    Again:
    Do not forget to 'Click' on the * found in the lower-left part of post #8 to thank the proposal.

  13. #13
    Registered User
    Join Date
    03-04-2014
    Location
    Limerick, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Removing the Selected Item in a ComboBox

    Hello,

    I have clicked on the on the * and left a comment about the excellent help you gave me for this problem. Can you see the comment?

  14. #14
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Removing the Selected Item in a ComboBox

    Quote Originally Posted by MacroPete View Post
    Hello, I have clicked on the on the * and left a comment about the excellent help you gave me for this problem. Can you see the comment?
    Thanks for that.

+ 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. Replies: 1
    Last Post: 01-18-2020, 12:44 AM
  2. Removing Item from ComboBox on CommandButton Click
    By hollyeva in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2015, 11:06 PM
  3. selecting an item in combobox, the item does not get selected
    By AVM in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-14-2014, 05:14 AM
  4. Replies: 3
    Last Post: 04-02-2013, 06:02 PM
  5. Combobox: How to determine the location of the selected item if combobox has rowsource
    By ahsanzafar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-11-2012, 03:24 PM
  6. Value of selected item in Combobox
    By Boris88 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2011, 09:56 AM
  7. ComboBox selected item
    By ballabali in forum Excel General
    Replies: 1
    Last Post: 12-15-2005, 11:55 AM

Tags for this Thread

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