Advertisement
ferdinand

[Q&A] search macro in excel using VB

Feb 25th, 2012
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [Tutorial] Find Excel (Macro)
  2. http://pastebin.com/RfBZEW00
  3. Search a value in column &copy row...[Excel VBA]
  4. http://pastebin.com/di3tdZEJ
  5.  
  6. Question:
  7. I have a Excel woorksheet with columns and rows that contains different types of data. I want to create a search macro using VBA in Excel, so when I write a criteria in a textbox and then click on a commandbutton, I want the macro to search throw a specified column and find that criteria and then write the data
  8.  that contains that row in textboxes, for example if I have 6 colums and 10 rows that contains data and in the first colum I have numbers, so if I serach for number 5 I want all data in the row to show in 6 differents textboxes.
  9.  
  10. Answer:
  11. Private Sub SearchButton_Click()
  12.      
  13.     SearchString = InputBox("Enter Search String", "Search")
  14.      
  15.     If SearchString = "" Then Exit Sub
  16.      
  17.     For Each c In Range(myRange)
  18.         If InStr(LCase(CStr(c)), LCase(SearchString)) Then
  19.             Texbox1 = Cells(c.Row, 1)
  20.             Texbox2 = Cells(c.Row, 2)
  21.             Texbox3 = Cells(c.Row, 3)
  22.             Texbox4 = Cells(c.Row, 4)
  23.             Texbox5 = Cells(c.Row, 5)
  24.              
  25.         End If
  26.     Next c
  27.      
  28.     MsgBox "Reached End of Report", , "Search Ended"
  29.      
  30. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement