+ Reply to Thread
Results 1 to 4 of 4

textbox search for listbox with an array

  1. #1
    Registered User
    Join Date
    07-31-2016
    Location
    Auckland New Zealand
    MS-Off Ver
    2013
    Posts
    14

    textbox search for listbox with an array

    I have a userform with a list box " lstLookup2" and a textbox "txt21"
    My data is on a worksheet called "Data" it has columns A to R
    I know I have to use an array to fill the list box because it has more than 10 columns but I know nothing about arrays
    The txt21 will have a value in it which is in column Q
    I want to look up column Q and return all rows that contain that value all columns
    For 2 days I have been trying heaps of code but I could get nothing to work in the end I gave up and decided to ask and now I have no code at all
    can somebody help me please

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: textbox search for listbox with an array

    There are several ways to achieve what you want.
    This is my simplistic approach:

    When txt21 changes
    - apply auto-filter to values in sheet "Data" (with criteria = txt21 value)
    - copy filtered values to sheet "FilteredList"
    - assign "FilteredList" values to listbox

    test in attached workbook
    - enter A,B or C in textbox

    here is the code:
    Please Login or Register  to view this content.
    It is always best to start simple, but if your filtering requirements are more complex, let me know

    Notes
    CurrentRegion is used 3 times in the code
    - CurrentRegion is the contiguous range around any cell or range
    - each current region is bordered by the first blank row/column around any table of data
    - so do not leave column/row empty to make things look pretty!

    .Range("A1").CurrentRegion.Copy rng
    - copies the filtered data
    - pastes to rng
    - rng was set previously as cell A1 on "FilteredList"


    EDIT - spotted a typo - oops!
    It still works in this instance, but D1 should of course be R1
    Please Login or Register  to view this content.
    Please amend the VBA in the attached workbook
    Attached Files Attached Files
    Last edited by kev_; 10-09-2017 at 08:30 AM. Reason: Typo
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Registered User
    Join Date
    07-31-2016
    Location
    Auckland New Zealand
    MS-Off Ver
    2013
    Posts
    14

    Re: textbox search for listbox with an array

    I am wondering if you could help me with the next problem
    I have cmdAdd2 on the form along with cmdEdi2, cmdDelet2e cmdReset2
    but right now I am trying to get the add button to work.
    This is a multi page form and this is page 2 all the controls work on page 1
    so I asumed that If I copied the code and renamed the cmd buttons with a 2 then they would work I put the clear filter in because the datasheet was still filtered from the code you gave me and I thought that was throwing things out.
    here is the code I have txt11 is a number that has to be entered
    it is supposed to put in the data from the txtboxs on the form into the sheet but debugs and says could not find specified object
    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: textbox search for listbox with an array

    Away from PC - so cannot test.

    It's a good habit to declare all variables including type
    - you have omitted to do that with most of them
    - VBA is able to help more if it is told what each variable should hold

    nextrow is correctly Set as a range on 2 lines (as declared)
    But this line
    nextrow = Me.Controls("txt" & x).Value
    Do you mean
    nextrow.Value = Me.Controls("txt" & x).Value

    Which line is highlighted when the code fails?

+ 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] Search within a ListBox as you type in a TextBox
    By Boechat in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-16-2016, 05:39 PM
  2. excel - search in listbox via textbox change?
    By rdm34 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-17-2015, 09:19 PM
  3. VBA - Adding Search to Listbox based on Textbox value
    By nimesh29 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-09-2014, 06:47 PM
  4. Search listbox from textbox
    By zplugger in forum Excel General
    Replies: 12
    Last Post: 08-23-2012, 08:11 PM
  5. [SOLVED] Textbox to search listbox
    By zplugger in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-22-2012, 08:25 AM
  6. Textbox search and display results in listbox
    By AirBrun in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 08-02-2012, 01:22 PM

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