+ Reply to Thread
Results 1 to 11 of 11

Error "ListIndex = -1"

  1. #1
    Registered User
    Join Date
    07-01-2015
    Location
    Antwerp
    MS-Off Ver
    2019
    Posts
    72

    Error "ListIndex = -1"

    Hi, I have a userform with a combobox and listboxes, coded as shown below. See Excel file in attachment.
    The userform is linked to a named table in worksheet "Data". The code returns an error when the named table is empty, ie. when the ListIndex = -1, ie. when the DatabodyRange is 0.
    I cannot seem to figure out a way to make this code errorproof when the table does not contain any data/listrows. Do any of you know?

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    09-03-2016
    Location
    Catania, Italy
    MS-Off Ver
    Excel 2019
    Posts
    182

    Re: Error "ListIndex = -1"

    Hello
    Try this
    Please Login or Register  to view this content.
    Bye bye,
    Marius

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Error "ListIndex = -1"

    use ListRows property to get number of rows.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

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

    Re: Error "ListIndex = -1"

    .List expects an array

    Please Login or Register  to view this content.
    Torachan,

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

  5. #5
    Registered User
    Join Date
    07-01-2015
    Location
    Antwerp
    MS-Off Ver
    2019
    Posts
    72

    Re: Error "ListIndex = -1"

    Thanks for your answers guys but all of the above suggestions return Runtime errors 9 or 91 ((LObj.DataBodyRange.Value) = <Object variable or With block variable not set>) when typing something in the combobox.
    Any idea why?

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

    Re: Error "ListIndex = -1"

    Get rid of all the spurious code in your ComboBox1_Change event.

  7. #7
    Registered User
    Join Date
    07-01-2015
    Location
    Antwerp
    MS-Off Ver
    2019
    Posts
    72

    Re: Error "ListIndex = -1"

    Hi torachan, could you specify which code is spurious? I deleted the entire Combobox1_Change event and this returns Runtime error 9 - Subscript out of range ("vbTextCompare = 1"). My VBA knowledge is limited.

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

    Re: Error "ListIndex = -1"

    As you are just moving items from sheet to combobox then to listbox their location is always known, there is no need for search routines or "vbTextCompare" just use the ordinates.
    attached is what I think you are trying to achieve.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    07-01-2015
    Location
    Antwerp
    MS-Off Ver
    2019
    Posts
    72

    Re: Error "ListIndex = -1"

    Thanks for the reply Torachan but your code unfortunately removed 2 crucial features of the userform: being able to search in the combobox by typing the first letters (autocomplete search); and being able to type a new entry in the combobox which is then also added into the named table.

    The user only uses the userform. The data sheet is hidden and not meant for manual editing.

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

    Re: Error "ListIndex = -1"

    give this a try
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    07-01-2015
    Location
    Antwerp
    MS-Off Ver
    2019
    Posts
    72

    Re: Error "ListIndex = -1"

    Thanks Torachan!

+ 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. Run-time error "Method "Add" of object "Attachments" failed" - HELP
    By Jyggalag in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2022, 10:03 AM
  2. [SOLVED] Multiple UserForms: ComboBox "Remembers" Previously Selected ListIndex Value
    By Xtab in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2018, 04:57 PM
  3. Replies: 35
    Last Post: 01-13-2016, 02:16 AM
  4. = EMBED("Forms.ComboBox.1","") error "Reference not valid."
    By CatharinaCatharina in forum Excel General
    Replies: 2
    Last Post: 12-11-2014, 09:58 AM
  5. [SOLVED] If Not ActiveSheet.Range("A1").Value Like "apple" Then MsgBox "Error"
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-18-2014, 02:16 PM
  6. [SOLVED] How to Count number of "Error" and "OK" after the word "Instrument" found in table row
    By eltonlaw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2012, 06:26 AM
  7. What is Error "Method "Paste" of object "_Worksheet" failed?
    By vat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2006, 04:10 PM

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