+ Reply to Thread
Results 1 to 6 of 6

Run-Time Error 380 in code to search for a word in Listbox through a textbox on Userform

  1. #1
    Registered User
    Join Date
    01-29-2022
    Location
    Delhi, India
    MS-Off Ver
    2013
    Posts
    4

    Run-Time Error 380 in code to search for a word in Listbox through a textbox on Userform

    Dear Sirs,

    I am new to Excel VBA. I came across this video on YouTube, which describes a code for running a search in a Listbox for a keyword typed into a Textbox on the same Userform. The code seemed perfect for my project. But I found it throws up Run-Time Error 380 on line 7.
    I give below the code I copied from the video and I have indicated where the error occurs.
    I am using Excel 2013 on Windows 10.

    Would you experts please help me. Thank you.

    My code:

    Private Sub TextBox1_AfterUpdate()
    [INDENT]'Add Column Headers
    With Me.ListBox1
    .Clear
    For ColHead = 1 To 6
    .AddItem
    .List(0, ColHead - 1) = Sheet1.Cells(1, ColHead).Value ? '<<< Run-Time Error 380, Invalid Property Value
    Next ColHead

    ListRow = 1

    If IsDate(Me.TextBox1) Then
    FindVal = CDate(Me.TextBox1)
    ElseIf IsNumeric(Me.TextBox1) Then FindVal = Val(Me.TextBox1)
    Else
    FindVal = "*" & Me.TextBox1 & "*"
    End If

    LastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
    For ShRow = 2 To LastRow
    FindRow = Application.WorksheetFunction.CountIf(Sheet1.Rows(ShRow).EntireRow, FindVal)
    If FindRow > 0 Then
    For ListCol = 1 To 6
    .AddItem
    .List(ListRow, ListCol - 1) = Sheet1.Cells(ShRow, ListCol).Value
    [INDENT][INDENT]Next ListCol

    ListRow = ListRow + 1

    End If

    Next ShRow

    End With

    End Sub

    Please forgive me if the indentations are not right in the above code. I am doing this for the first time. Thank you for your patience.
    Last edited by Arif N. Khan; 12-31-2022 at 02:56 AM. Reason: Indents were not right.

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,215

    Re: Run-Time Error 380 in code to search for a word in Listbox through a textbox on Userfo

    Please see the yellow banner at top of the page on how to attach a sample workbook.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Run-Time Error 380 in code to search for a word in Listbox through a textbox on Userfo

    Please Login or Register  to view this content.
    This code above is adding column headers to the list box. I'm confused. Why are you adding headers when the stated purpose was to search the listbox?
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    01-29-2022
    Location
    Delhi, India
    MS-Off Ver
    2013
    Posts
    4

    Re: Run-Time Error 380 in code to search for a word in Listbox through a textbox on Userfo

    Hi AlphaFrog,

    Thank you for your reply. The column headers need to be displayed in the listbox because this listbox is to be part of a bigger userform in which records from a data set have to be shortlisted by searching a keyword in any of the fields in the listbox.

    My problem is that the code I am using works well for a data set of 6 columns, but when I use it on a data set that has 11 columns, it begins to throw up runtime errors.

    I am uploading an Excel file I have prepared to explain the situation to you. The workbook has two sheets each with a corresponding userform with the identical code in both, except for the column count as 6 for one sheet and 11 for the other with the code and the control properties set accordingly.

    I hope this explanation will clear the confusion my first post created. Please do let me know if want any further clarification. Thank you for your help.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-29-2022
    Location
    Delhi, India
    MS-Off Ver
    2013
    Posts
    4

    Re: Run-Time Error 380 in code to search for a word in Listbox through a textbox on Userfo

    Thank you for your message and kind help, John Topley.

    I will do as you have advised.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Run-Time Error 380 in code to search for a word in Listbox through a textbox on Userfo

    A maximum of 10 columns can be added to a listbox with the VBA AddItem method. However, a listbbox can be populated with more than 10 columns using an array. So first populate an multicolumn array with your search results. Then populate the listbox with that array.


    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 01-11-2023 at 01:13 AM.

+ 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. [SOLVED] TextBox Filter-Down Search of UserForm ListBox Populated with Worksheets
    By RoroBear in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-18-2021, 01:24 AM
  2. [SOLVED] [Userform] How to search data in Listbox from textbox with more than 10 columns
    By ccmyccmy in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-25-2020, 03:39 AM
  3. VBA Code for Partial Search Excel Listbox with Textbox Change
    By mahmudayaz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-09-2017, 06:14 AM
  4. Want to one more textbox to search the data in listbox (Userform)
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-02-2017, 08:00 AM
  5. [SOLVED] Vba assistance with UserForm Textbox search code
    By sintek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-05-2016, 09:02 AM
  6. [SOLVED] VBA userform search for listbox entry based on Textbox value
    By Hilton1982 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-12-2015, 08:29 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