?
Solved

Search Box

Posted on 2016-10-17
13
Medium Priority
?
64 Views
Last Modified: 2016-10-20
Can any one tell me why this code is not working. I want it to search my spreadsheet and display the data from select rows if the correct number is entered into the search field. Basically a basic search engine.



Private Sub CommandButton1_Click()
Row_Number = 1
Do
DoEvents
Row Number = Row_Number + 1


item_in_review = Sheets("Blueteq").Range("B" & Row_Number)

If item_in_review = TextBox1.Text Then


TextBox2.Text = Sheets("Blueteq").Range("O" & Row_Number)

TextBox2.Text = Sheets("Blueteq").Range("K" & Row_Number)

TextBox2.Text = Sheets("Blueteq").Range("L" & Row_Number)

End If

Loop Until item_in_review = ""


End Sub
0
Comment
Question by:Sam Coombes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 41847079
Add item_in_review = "" before the End If.
0
 

Author Comment

by:Sam Coombes
ID: 41847163
Thank you I have now tried this but an occurs with the row number = 0

Says can't find project or library
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 41847166
Can you attach your workbook?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:MacroShadow
ID: 41847169
In addition to what Martin mentioned,
1. There is no need for DoEvents in your loop. Looping rows is so fast there is no need to yield the processor for other tasks.
2. Row Number = Row_Number + 1 should be at the end of the loop, i.e. one line before the Loop Until line.
3. You are overwriting TextBox2 so at the end you will only see the data from the L row.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41847172
Can't find project or library, usually means there is a missing reference in the file.
In the IDE goto the Tools menu, then select References, if any are marked "Missing", remove them.
Then Goto the Debug menu and click Compile VbaProject.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 41847201
And you don't need to loop (and shouldn't because it a lot slower).

Private Sub CommandButton1_Click()
    Dim rngFound As Range
    With Sheets("Blueteq")
        Set rngFound = .Columns("B:B").Find(What:=TextBox1.Text)
        If Not rngFound = Nothing Then
            TextBox2.Text = .Range("O" & rngFound.Row) & " " & _
                         .Range("K" & rngFound.Row) & " " & _
                         .Range("L" & rngFound.Row)
        Else
            MsgBox "Not found"
        End If
    End With
End Sub

Open in new window

0
 

Author Comment

by:Sam Coombes
ID: 41847423
That's amazing thank you so much, but why does it now say there is an error with
 
'If Not rngFound = Nothing Then' highlighting an issue with the nothing statement
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 41847432
Can you attach your workbook as it is now?
0
 

Author Comment

by:Sam Coombes
ID: 41847853
Thank you I have attached the spreadsheet which looks a mess because I have removed all of the other sheets for governance reasons. Also there is no real data on the sheet you have. High-Cost-Drugs-Database-2015---201.xlsm
0
 
LVL 49

Accepted Solution

by:
Martin Liss earned 2000 total points
ID: 41848430
Replace the sub with this.

Private Sub CommandButton1_Click()

    Dim rngFound As Range
    With Sheets("Blueteq")
        Set rngFound = .Columns("B:B").Find(What:=TextBox1.Text)
        If Not rngFound Is Nothing Then
            TextBox2.Text = .Range("O" & rngFound.Row) & " " & _
                         .Range("K" & rngFound.Row) & " " & _
                         .Range("L" & rngFound.Row)
        Else
            MsgBox "Not found"
        End If
    End With

End Sub

Open in new window


Given a search for 1777, what exactly would you like to see in Textbox1?
1
 
LVL 49

Expert Comment

by:Martin Liss
ID: 41850411
Sam, did my previous pos resolve your question?
0
 

Author Closing Comment

by:Sam Coombes
ID: 41851487
Brilliant thank you so much.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 41852312
You're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question