dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Run-Time Error 381

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    7

    Resolved [RESOLVED] Run-Time Error 381

    Hello. I am using VBA for Excel 2003. I am trying to loop through a list of items in a listbox one by one to see if any of the entries in the listbox equal a certain value. I've got the loop part sorted out, and have placed each list entry into a variable to be tested one by one. Part of the code is shown below (code is shown in blue, comments in green):


    For lstIndex = 0 To List1.ListCount
    ' For lstIndex 0 to the number of entries in the listbox

    CheckEntry = List1.List(lstIndex)
    ' Move the list entry into the variable "CheckEntry" where the particular list entry is defined by lstIndex which increments of course by 1 every time the loop runs

    If CheckEntry = "Hello"

    lstFinalWordList.AddItem CheckWord
    ' Output the Value of "Checkword" to another list box if the condition is met

    End If

    Next lstIndex


    The problem is when the loop reaches the last entry in the listbox and tries to put it into the variable "CheckEntry", The following error occurs:


    Run-time error '381':
    Could not get the List Property. Invalid property array index.


    I dont understand how the loop works for all but the last item in the listbox. It dosent matter whether or not i use the ListCount property to indicate the end of the list or if i use a standard number, the same error occurs. The Line of code the program dosent like for the last listbox entry is of course:


    CheckEntry = List1.List(lstIndex)


    I would appreciate any help offered to solve this problem. Thankyou.

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Run-Time Error 381

    Suppose you have 3 items.

    The list starts at 0. Therefore the index of item 1 is 0. Therefore the index of item 2 is 1. The index of item 3 is 2.


    Hence you don't want to loop to list1.listcount, because the listcount is 3, but there is no item three. The last item is number 2.


    Understand?


    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    7

    Re: Run-Time Error 381

    Yeh good call zaza, thanks a lot, i forgot about that. That explains why it didnt work even when i used a standard number instead of using the listcount property. Cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width