+ Reply to Thread
Results 1 to 15 of 15

Run-time error when reordering items in tables (ListObject)

  1. #1
    Registered User
    Join Date
    07-04-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    9

    Run-time error when reordering items in tables (ListObject)

    Hi there, I encountered some annoying run-time errors when reordering items. I am using Excel 2010

    I have a UserForm containing a ListBox, whose RowSource is set to a named table (ListObject). I would like to allow the user to reorder the items in the list by clicking up/down buttons, as well as the functionalities to add/remove. It works well sometimes, but in many cases I'll see a run-time error at the line
    Please Login or Register  to view this content.
    . Sometimes it's run-time error 1004 saying some methods (add/clear/cut etc.) failed. Sometimes it tells me the object invoked is disconnected from the clients... I guess I have done something really wrong but can't figure it out. Can anyone help? thanks in advance.

    Here is a subroutine of a Down button in my UserForm:

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Are the errors happening in the userform?

    If this is the case it's probably because you have used RowSource to populate the listbox and are cutting/inserting from the row source.

    Instead of cutting/inserting etc why not swap the values?

    Here's a simple example that swaps cells in row 5 and 6 of column A.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-04-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Run-time error when reordering items in tables (ListObject)

    Hi Norie, thanks a lot for your help! I just tried and it worked well.

    One more question remained here. Sometimes the Add method of ListObject will trigger similar run-time error, too, saying something like 'Add method failed'. Is that caused by a similar problem, since I use table as RowSource for a ListBox in UserForm?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error when reordering items in tables (ListObject)

    Anything that you do the row source, in this case a table/list object, will affect the Listbox and could cause problems.

    How exactly are you adding to the table?

  5. #5
    Registered User
    Join Date
    07-04-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Run-time error when reordering items in tables (ListObject)

    I append the table/ListObject to add new items to ListBox. Can I set RowSource to nothing, call the Add method, and then set RowSource back, to solve these problems?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    How do you append?

  7. #7
    Registered User
    Join Date
    07-04-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Run-time error when reordering items in tables (ListObject)

    Hi, I just re-produced an error when removing an item.
    The error message: 'run-time error 1004, application-defined or object-defined error' at the line '.ListRows(i).Delete'

    Actually may I know more underlying details about the problem using RowSource and ListObject?
    Thanks a lot for your help in advance!

    My code:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    07-04-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Run-time error when reordering items in tables (ListObject)

    And similar error (Add method failed) can be seen in this code at line '.ListRows.Add':

    Please Login or Register  to view this content.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error when reordering items in tables (ListObject)

    The problem with using RowSource is that it's a kind of two way thing.

    Everything you do to the list on the userform is reflected in the list on the worksheet, and vice versa.

    Normally that's OK but sometimes it causes problem.

    What you can do is use List to populate the listbox in the Initialize event.

    Something like this.
    Please Login or Register  to view this content.
    The only problem with that is that when you update the list on the worksheet you'll need code to update the list on the userfrom, and vice versa.

  10. #10
    Registered User
    Join Date
    07-04-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Run-time error when reordering items in tables (ListObject)

    Hi Norie, thank you very much! I'll try to use List in the future.

    But it does not solve my problem of 'Add' and 'Delete' methods failures. I did not use ListObjects("Selected") as RowSource of any ListBox. May I know what can cause such failures?

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error when reordering items in tables (ListObject)

    I though you were still talking about errors with the listbox.

    I think I need to see the workbook to help with errors with the ListObject.

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.


    One thing though is that when deleting in a loop you should always start from the bottom and work up.

  12. #12
    Registered User
    Join Date
    07-04-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Run-time error when reordering items in tables (ListObject)

    Thanks again Norie. However I am afraid I cannot upload the entire workbook since the data are kind of sensitive... But as you can see in #7 and #8, .ListRows(i).Delete and .ListRows.Add fails sometimes, despite the fact that the ListObject "Selected" is not used as RowSource of any ListBox. Could you come up with some possible reasons and maybe suggest some approaches I can take to debug?

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error when reordering items in tables (ListObject)

    There could be a lot of reasons but I wouldn't be able to tell you what the problem is without an example workbook.

    If there is sensitive data remove it, or, better, replace it with dummy data.

  14. #14
    Registered User
    Join Date
    07-04-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Run-time error when reordering items in tables (ListObject)

    [Attachment Removed]

    Let me try attachment... I removed most unrelated code.

    One way to reproduce the problem:

    1. Click Edit Group
    2. Add some groups named "Group 1", "Group 2" etc.
    3. Reorder the group list by clicking Up and Down buttons
    4. Add a new group
    Last edited by Cihan Chen; 07-16-2013 at 05:08 AM.

  15. #15
    Registered User
    Join Date
    07-04-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Run-time error when reordering items in tables (ListObject)

    Hi Norie, any luck with the code? I know it is not good to ask for help like this. It is all fine if you don't have time, thanks a lot.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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