+ Reply to Thread
Results 1 to 17 of 17

error on RemoveItem from Listbox

  1. #1
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    error on RemoveItem from Listbox

    I am getting an error on RemoveItem. How to solve?

    Also despite having 2 columns, there is only one showing up. How to display the item # in column 1, keeping the item in Column 2?


    Private Sub ListBox1_Initialize()
    Dim Mylist(81, 2) As String ' List of Merged Cells after Set Up (manual or automatic) is over
    Dim i As Long

    Mylist(0, 0) = "Line"
    Mylist(0, 1) = "Column BA"

    For i = 1 To 81
    Mylist(i, 0) = "#" & FormatNumber(i, 0) ' number of insertion of Merged Cells
    Mylist(i, 1) = "BA" & FormatNumber(i, 0) ' Where the Merged Cells addresses are located
    Next i

    ListBox1.ColumnCount = 2
    ListBox1.List = Mylist
    End Sub

    '+++++++++++++++++++++++++++++++++++++++++++++++ When User clicks on one item in the list, then the value listed will do µnmerge in the Main Grid +++++++++++++++++++++++++++++++++++
    Private Sub ListBox1_Click()

    Range("AI49").Value = UserForm4.ListBox1.Value

    Analyze_Which_to_UnMerge ' unMerge the selected item stored in AI49, it will also reduce A45 by one
    Range("A45") = Range("A45") + 1

    UserForm4.ListBox1.RemoveItem Range("A45").Value

    Range("A45") = Range("A45") - 1
    End Sub

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: error on RemoveItem from Listbox

    I've had to change my answer. I can't determine what's happening.

    If your value in A45 is higher than your listcount, you'll get an error.
    Last edited by Tinbendr; 02-12-2016 at 07:52 AM.
    David
    (*) Reputation points appreciated.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: error on RemoveItem from Listbox

    @Loski.
    Please read the forum rules. In your case here, the rule about code tags.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: error on RemoveItem from Listbox

    ListBoxes don't have an Initialize event.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: error on RemoveItem from Listbox

    no I checked, A45 is the number of items stored in the Listbox. Its value is always correct. I checked. I am trying to remove the last one only, the right item gets always UnMerged OK but does not get removed from the list

  6. #6
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: error on RemoveItem from Listbox

    ah ok will change that. thanks

  7. #7
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: error on RemoveItem from Listbox

    oops!
    will read
    sorry

  8. #8
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: error on RemoveItem from Listbox

    ListBoxes don't have an Initialize event.
    Good catch. I read right past this.

    I've never seen someone track a listbox that way.

    If you're only removing the last item, then

    Please Login or Register  to view this content.
    Or the one that's selected.

    Please Login or Register  to view this content.

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: error on RemoveItem from Listbox

    The next effect of this is to decrease the number of items in the box, and leave A45 unchanged.
    FirstLine incriments A45. SecondLine decrements A45. Net effect = 0
    Please Login or Register  to view this content.
    Also you should make sure that A45 is between 0 and ListBox1.ListCount - 1

  10. #10
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: error on RemoveItem from Listbox

    ? I dont get it.
    the reason I have to increase A45 by one before doing the RemoveItem is because A45 got decreased by one in Analyze_Which_to_UnMerge as spoecified in the text.
    otherwise i would remove the wrong item
    And after that RemoveItem I do have to decrease A45 by one. A45 is the qty of items in the list used in many other subs in my program.
    So that code is perfectly correct.
    and A45 is always between 0 and ListCount - 1 , thanks for your concern

    despite all the suggestions, I still get an error runtime !!! strange

  11. #11
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: error on RemoveItem from Listbox

    How are you filling Listbox? If it's via Rowsource, it will error if you try to remove an item.

  12. #12
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: error on RemoveItem from Listbox

    UserForm4.ListBox1.AddItem "A1"
    I am developing a Sudoku solving program. it has 729 cells (9x9x9). each group of 9 cells have a name A1 to I9 (81 in total).
    all that works fine for months. I give the user the capability of unmerge a group. Among those capabilities i wanted to add a 3rd way, unmerging from the list box. it does unmerge right but the list box keeps the item in.
    so i tried to use RemoveItem, 1st time I try that in my life but it does do what I wish and get an error.

    today when I changed the RemoveItem as follows I dont get an error anymore like I did using some suggested solutions like .ListIndex or ListCount - 1
    UserForm4.ListBox1.RemoveItem UserForm4.ListBox1.MousePointer
    but the whole Listbox gets empty not only the one pointed. No error but still Strange.

    RowSource? not sure

  13. #13
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: error on RemoveItem from Listbox

    RowSource? not sure
    Oh, you'd know it if you were using it. It's a property of the listbox and you can either manually enter a range, or assign one via code.

    E.g.
    Please Login or Register  to view this content.
    Hmm. I'm out of ideas. You'll just have to put in a break, debug.print listbox.listindex to the immediate window and watch what happens.

  14. #14
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: error on RemoveItem from Listbox

    by the way the Listbox1_Initialize never run. It never got running. I removed that code entirely w/o any negative effect.

    may be one way to solve instead of using the contents, would be using the item # that I dont presently have in the Listbox. it only has A1 to I9 listed minus those locations merged while charging the sudoku (about 18 to 35).
    how may i had the item # in the listbox? it would become an array, right?

  15. #15
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: error on RemoveItem from Listbox

    may I add (not had). I broke my arm last week so I type with only one finger

  16. #16
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: error on RemoveItem from Listbox

    That's what Mike said in #4; there IS NO initialize event for the control Listbox. That's a event for the userform itself. (You can always double click a control and the default code will automatically be entered. Then at the top right dropdown of the window, there is a list of events applicable to that control. Unless you write your own class module to add it.)

    When is the decrement suppose to occur? When you click on the listbox? Or do you select an item in the list? Or when you double click an item?

  17. #17
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: error on RemoveItem from Listbox

    hello,
    I did find an alternative last week. I added the storing of the items into a range. Then when an item is clicked into the listbox, that item gets removed in tha range also, then I clear the listbox.
    Finally I copy the contents of the range and do additem in the listbox to regenerate it.
    It works like a charm.
    no need to try using RemoveItem anymore, my alternative solution does it.

    thanks for your tries,
    cordially
    daniel

+ 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. removeitem and listindex
    By nparsons75 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-26-2014, 07:32 AM
  2. [SOLVED] RemoveItem failed?!
    By ExcelWizards in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2013, 05:55 PM
  3. Error 424 Object Required Error when Using AddItem to ListBox Generated in Code
    By jclark419 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2010, 12:47 PM
  4. RemoveItem from ComboBox
    By CrazyFileMaker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-01-2009, 04:42 AM
  5. RemoveItem
    By wmorrison49 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-30-2008, 04:05 PM
  6. [SOLVED] RemoveItem resetting Value in ComboBox
    By Drummer361 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2006, 01:55 PM
  7. MSFlexGrid RemoveItem
    By gti_jobert in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2006, 06:30 AM
  8. RemoveItem - ComboBox
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-16-2006, 11:55 AM

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