Results 1 to 8 of 8

Thread: [RESOLVED] Searching a column of data

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    21

    Resolved [RESOLVED] Searching a column of data

    I need to be able to search a column for a certain last name. When i find that name, the data associated with it, which is horizontally listed in a spreadsheet will transfer to a textbox in a userform. Anybody have any ideas on how to go about this?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Searching a column of data

    use the find method to set a range based on the range of the column you want to search
    vb Code:
    1. set rngfound = range("c:c").Find("smith") ' other arguments
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Searching a column of data

    Here is the row you are looking for:
    Code:
    dim aRow as long
    dim i as Integer
    
    aRow = rngfound.Row
    i = 1  'Init for Column A data if you write a loop
    Now you can do a loop or a linear set of instructions copying "Cells(arow, i).Value 'for Col A" etc. into your textboxes on your form.

    Does that help?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    21

    Re: Searching a column of data

    I tried placing "Cells(arow, i).Value 'for Col A" in the Textbox properties. I pasted it in the Name and Data text locations. Neither one worked properly. Where should I paste it then?

  5. #5
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Searching a column of data

    I was under the impression that you are trying to write VBA code for a Macro. That's what this forum is all about ... "Visual Basic".

    Cells(arow, i).Value is part of a VBA instruction that fetches the value out of a cell on the sheet. Once you have that value, you have to have code to paste it into the proper text box. You will need to know how to reference the form and textbox in code. That isn't hard, but it depends on the Form Name property and the TextBox Name property.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Searching a column of data

    vb Code:
    1. textbox1.text = sheets("sheet1").cells(arow,1).value  'from col A
    2. textbox2.text = sheets("sheet1").cells(arow,2).value  'form col B
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    21

    Re: Searching a column of data

    Thanks guys!!! Hey could I put the same text in two seperate userforms? I tried this code but it just takes me too Userform2.
    vb Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3. Dim aRow As Long
    4. Dim i As Integer
    5. Dim rngfound As Variant
    6.  
    7.  
    8. Set rngfound = Range("B:B").Find(LName.Text) ' other arguments
    9.  
    10.  
    11.  
    12. aRow = rngfound.Row
    13.  
    14.  
    15.  
    16. FName.Text = Cells(aRow, 1).Value 'for Col A
    17. LName.Text = Cells(aRow, 2).Value 'for Col B
    18.  
    19. UserForm2.Show
    20. firstname.Text = Cells(aRow, 1).Value 'for Col A
    21. LastName.Text = Cells(aRow, 2).Value 'for Col B
    22.  
    23.  
    24.  
    25. End Sub

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Searching a column of data

    vb Code:
    1. userform2.firstname.Text = Cells(aRow, 1).Value 'for Col A
    2. userform2.LastName.Text = Cells(aRow, 2).Value 'for Col B
    3. UserForm2.Show
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width