+ Reply to Thread
Results 1 to 8 of 8

Search Down Column, Activate Row from Columns A:X and Add Selection to Listbox

  1. #1
    Registered User
    Join Date
    12-21-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    4

    Search Down Column, Activate Row from Columns A:X and Add Selection to Listbox

    Hello! Fairly new to VBA and appreciative of any help! Basically, I am trying to search down a column on a spreadsheet and add the rows that correspond to the search criteria into a listbox.

    Objective:
    1. User enters number on UserForm in text box
    2. User hits "Generate Report" button and the macros searches down column C of the active spreadsheet (there is only one worksheet)
    3. Once a match is found, the entire row is selected from columns A to X and the row added to a listbox on the UserForm

    Problems:
    1. I have been able to code and display the what I searched for in the first column, but not the entire row using the code. This finds the correct number of entries, but displays them in the first column of the listbox only where I want the entire row displayed.

    Private Sub cmdGenerateReport_Click()
    Dim x As String
    strSearch = LotNumberTxt.Text
    A = 3
    Do
    x = Cells(A, 3).Value
    If x = "" Then Exit Sub
    If x = strSearch Then Me.ListBoxReport.AddItem x
    A = A + 1
    Loop Until Cells(A, 3).Value = ""

    End Sub


    2. When I activate the row from columns A to X the "Object Required" Error comes up even though I defined y (new variable) as a range.

    Private Sub cmdGenerateReport_Click()
    Dim x As String
    Dim y As Range
    strSearch = LotNumberTxt.Text
    A = 3
    Do
    x = Cells(A, 3).Value
    If x = "" Then Exit Sub
    If x = strSearch Then Cells(A, 3).Select
    Set y = Range("A" & ActiveCell.Row & ":X" & ActiveCell.Row).Select
    A = A + 1
    Loop Until Cells(A, 3).Value = ""

    End Sub



    So, if anybody can help me select the range and add it to the listbox (I thought AddItem would work but it does not seem to) that would be great! Any suggestions are appreciated. Thanks!

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Search Down Column, Activate Row from Columns A:X and Add Selection to Listbox

    You can set up multi column listboxes easily enough and use the additem to add a row of data but you have to refer to the specific location in the lisbox numerically much as you world for a 2d array or using the cells keyword on the worksheet.

    Once you've found your keyword on the worksheet you need a for next routine to step through each cell/column and add it to the requisite location in the listbox

    To populate a new row of data for a "found" record The Syntax would be :-
    Please Login or Register  to view this content.
    You would need to have the listbox columncount set to the required number of columns (along with each column width
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    12-21-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Search Down Column, Activate Row from Columns A:X and Add Selection to Listbox

    Thank you very much for your reply! This helped a bit, adding what I wanted into the listbox, but the first column is blank and everything is shifted over one column. Also, for the Worksheet Row number I want to use a variable (A) so that when the program finds the search query that row is copied. When I add this variable for the row number it does not work. Also, though unrelated, I am getting a Loop Until Without Do Error even though there is clearly a Do in the code. There is probably an easy explanation for this, I am just not familiar. Here is what I have:

    Dim x As String
    strSearch = TextBox1.Text
    A = 3
    ListBox1.ColumnCount = Range("A:X").Columns.Count
    x = Cells(A, 3).Value
    Do
    If x = "" Then Exit Sub
    If x = strSearch Then
    Me.ListBox1.AddItem
    For ColumnNumber = 1 To 24
    Me.ListBox1.List(Me.ListBox1.ListCount - 1, ColumnNumber) = Cells(A, ColumnNumber)
    Next ColumnNumber
    A = A + 1
    Loop Until Cells(A, 3).Value = ""
    End If


    End Sub


    Sorry for writing the code out like this, I am new to this forum and have not been able to find the button to insert the code. Your help is greatly appreciated and I will continue to work at this

  4. #4
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Search Down Column, Activate Row from Columns A:X and Add Selection to Listbox

    Listbox Columns are indexed from 0 so

    Please Login or Register  to view this content.
    Would start the fill from the leftmost columns.

    If you don't need to display the contents of a particular column set the listbox column width to 0.
    To retieve the contents of a listbox "cell" the syntax is :-

    Please Login or Register  to view this content.
    To display you code as code in the forum :-
    1. Highlight the code
    2. Select the hashtag icon from the top menu #

    The Do Loop error is because the Endif is outside of the do loop but has to be inside ... try this ...

    Please Login or Register  to view this content.
    Last edited by AndyLitch; 12-31-2013 at 11:36 AM.

  5. #5
    Registered User
    Join Date
    12-21-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Search Down Column, Activate Row from Columns A:X and Add Selection to Listbox

    Thanks again! Getting closer, but now the search engine does not work. I have made a generic file to show my problem and have attached it here. I also have a header (the titles of each column) that should come up each time the button is clicked. However, this only works when the first column searched matches the search criteria. And then it displays all of the rows, not just the ones that match in "Column C". The code is:
    Please Login or Register  to view this content.
    In short, the results are in the right place now, but the right results are not displayed. Thanks for your help also with posting code in the forum.
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Search Down Column, Activate Row from Columns A:X and Add Selection to Listbox

    20131231_Example Worksheet1.xls

    There you go... If you need multiple lines of code to conditionally execute, you need to split your if then and endif statements.. See attached workbook.

    Please Login or Register  to view this content.
    Last edited by AndyLitch; 01-01-2014 at 04:25 AM.

  7. #7
    Registered User
    Join Date
    12-21-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Search Down Column, Activate Row from Columns A:X and Add Selection to Listbox

    Fantastic! Thank you very much for your help, this works great!

  8. #8
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Search Down Column, Activate Row from Columns A:X and Add Selection to Listbox

    Happy New Year

+ 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. Listbox Selection True for Certain Values on Activate
    By jvbeats in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-07-2012, 03:25 PM
  2. Populate a Userform from a selection on a popup search listbox
    By ahmadassaad in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 10-16-2012, 09:00 AM
  3. Hid Columns based on ListBox Selection
    By panteradrummer66 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-14-2012, 10:42 AM
  4. Multi select Listbox Items selection based on other Listbox item selection.
    By srinivassathi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-20-2011, 05:53 AM
  5. Replies: 5
    Last Post: 06-05-2007, 10:06 AM

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