Consulting

Results 1 to 7 of 7

Thread: Finding data in a range

  1. #1
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location

    Finding data in a range

    I'm using the following code to search for the value of a Textbox in a range and if that value is found then the range is displayed in a Listbox;

    [vba]Dim Rng As Range, Dn As Range
    Dim Fd As Boolean
    ListBox1.Height = 100

    With Sheets("6 Month Data")
    Set Rng = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
    End With

    With ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "30,75,75,30"
    End With
    For Each Dn In Rng
    If Dn.Value = Val(TextBox1) Then
    With ListBox2
    .AddItem Dn
    .List(.ListCount - 1, 1) = Dn.Offset(, 1).Text
    .List(.ListCount - 1, 2) = Dn.Offset(, 2).Text
    .List(.ListCount - 1, 3) = Dn.Offset(, 3).Text
    .List(.ListCount - 1, 4) = Dn.Offset(, 4).Text
    Fd = True
    End With
    End If
    Next Dn
    If Fd = False Then MsgBox "Number " & TextBox1.Value & " is not found in the database for this period", vbInformation, "number not found"[/vba]

    I want to amend this slightly but I can't figure it out - I want to search for the value in column E, the data for which is a combination of text and numbers, (not sure if this makes a difference).

    I still want the Listbox to display the range from A:E but using column E as the range to search initially.

    I've tried amending the range line by putting in E etc but I keep getting the message that the value is not found, even though it's there.

    Have I missed something really obvious?

    Posted on Mr Excel too but I can't copy the hyperlink because their site seems to have gone down!
    Last edited by sharky12345; 07-06-2013 at 02:47 AM.

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    This Range
    [VBA]Set Rng = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))[/VBA] defines the search to Column A, so tell me what would you think to do the set the column to Column E?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    Rather than searching column A I want to search column E for the value, but it still doesn't find it. I've tried changing the code to "E1" and "E" but still no luck.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    What exactly is the value that you are searching for, and does it truely exist in column E? Remember that the value needs to be exact, no spaces ( trailing or otherwise.)
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    The value is 'MDT70598'.

    I have tried searching for it exactly but it is not found, despite it appearing hundreds of times.

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    You seriously need to consider attaching a sample workbook. I ilke many others cannot see over your shoulder to view to workbook you are currently working on. Click on "Go Advanced' scroll down to 'Manage Attachments" and follow the prompts from there
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    Banned VBAX Regular
    Joined
    Aug 2010
    Posts
    54
    Location
    I won't be attaching a sample workbook and have sent you a private message.

    Disregard this, I will work around the problem another way.

Posting Permissions

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