display onlyselected row in excel using macro

December 29, 2010 at 23:23:33
Specs: Windows XP

Could anyone help me in getting a VB code for the below requirement.

I have some rows in excel. I need to create a Search button in macro which will allow me enter the ID. On clicking the button it should display only the row corresponding to the ID and all others should be hidden.

For example,
Sample data
ID Name Place
1111 A India
2222 B India
3333 C India

When 2222 is entered it should display like

ID Name Place
2222 B India

See More: display onlyselected row in excel using macro

Report •

December 30, 2010 at 07:18:46
Assuming your table begins in A1 like this...

     A      B       C
1   ID     Name   Place
2  1111     A     India
3  2222     B     India
4  3333     C     India

...then this code should work for you:

Option Explicit
Sub HideRows()
Dim myID As String
Dim notFound As Integer
Dim c As Range
 myID = Application.InputBox("Enter ID Number", "ID Number")
  If myID = False Then Exit Sub
   With Columns("A")
    Set c = .Find(myID, lookat:=xlWhole)
      If Not c Is Nothing Then
        Rows(2 & ":" & Rows.Count).Hidden = True
        Rows(c.Row).Hidden = False
        Exit Sub
      End If
   End With
  notFound = MsgBox("ID Not Found.  Try Again?", vbYesNo)
   If notFound = vbYes Then GoTo getId:
End Sub

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.

Report •
Related Solutions

Ask Question