Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Need a macro to search and results in excel.

Hi,

I have uploaded a excel fine.I have all data in Sheet 1.I have the search fields in Sheet 2.What i want is when i enter any data in Sheet 2 colum 2.It has to copy the full lines found to a new sheet.
EX:
Say
I put 512 in the Ram box.It has to search the sheet 1 and copy all Rams with 512.
http://www.filepatio.com/3628

regards
Sharath
Avatar of Hitesh Manglani
Hitesh Manglani
Flag of India image

'Paste this code in sheet2 module
Private Sub Worksheet_Change(ByVal Target As Range)
static s2row
   if target.column=2 then
        For i=1 to Sheet1.UsedRange.Rows.Count
          For i=1 to Sheet1.UsedRange.Columns.Count
              if Sheet1.Cells(i,j).value = target.value then
                 Sheet1.Rows(i).Copy Destination:=Sheet2.Rows(s2row+1)
                 s2row=s2row+1
              End if  
          Next
        Next

End Sub

Avatar of bsharath

ASKER

did you check the file i attached.
it is in Excel 2007 format
'Check this works fine
Private Sub Worksheet_Change(ByVal Target As Range)
Static s2row
If Target <> "" Then
   If Target.Column = 2 Then
        For i = 1 To Sheet1.UsedRange.Rows.Count
          For j = 1 To Sheet1.UsedRange.Columns.Count
              If Sheet1.Cells(i, j).Value = Target.Value Then
                 Sheet1.Rows(i).Copy Destination:=Sheet3.Rows(s2row + 1)
                 s2row = s2row + 1
              End If
          Next
        Next
    End If
End If

End Sub



I get this.

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Expected: identifier
---------------------------
OK   Help  
---------------------------
did u paste this in the sheet2 module?
where r u getting the error in which line


---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Only comments may appear after End Sub, End Function, or End Property
---------------------------
OK   Help  
---------------------------


just delete everytinh in the module and paste this again
Private Sub Worksheet_Change(ByVal Target As Range)
Static s2row
If Target <> "" Then
   If Target.Column = 2 Then
        For i = 1 To Sheet1.UsedRange.Rows.Count
          For j = 1 To Sheet1.UsedRange.Columns.Count
              If Sheet1.Cells(i, j).Value = Target.Value Then
                 Sheet1.Rows(i).Copy Destination:=Sheet3.Rows(s2row + 1)
                 s2row = s2row + 1
              End If
          Next
        Next
    End If
End If

End Sub

Can you give the macro name too.
'you cannot name this macro because its an event
Private Sub Worksheet_Change(ByVal Target As Range)
Static s2row
If Target <> "" Then
   If Target.Column = 2 Then
        For i = 1 To Sheet1.UsedRange.Rows.Count
          For j = 1 To Sheet1.UsedRange.Columns.Count
              If Sheet1.Cells(i, j).Value = Target.Value Then
                 Sheet1.Rows(i).Copy Destination:=Sheet3.Rows(s2row + 1)
                 s2row = s2row + 1
              End If
          Next
        Next
    End If
End If

End Sub


When i run this it asks me a macro name to run
dont run thiss buddy, just type something in the Sheet2 , e.g 2 GB in RAM and see the results in sheet3
Tried this does not work
I should just paste your code in the sheet 2 and type the data you said am i correct?
I did the same but no results.
>I should just paste your code in the sheet 2
no dont paste the code in sheet2,
press Alt + F11 to open the VB Editor , double click Sheet2 on the left pane and on the right paste my code.
then type in sheet2
Ok got it.Is all the headers linked.When i try for ram,HDD,FDD its working in the machine name when i type it does not how
Once i put CDD in the box and i change to DVD the results are not copied.Can you crete new sheets for each search
Private Sub Worksheet_Change(ByVal Target As Range)
Static s2row
s2row = 1
Dim sh As Worksheet
Set sh = ActiveWorkbook.Sheets.Add(, ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count), , xlWorksheet)
If Target <> "" Then
   If Target.Column = 2 Then
        For i = 1 To Sheet1.UsedRange.Rows.Count
          For j = 1 To Sheet1.UsedRange.Columns.Count
              If Sheet1.Cells(i, j).Value = Target.Value Then
                 Sheet1.Rows(i).Copy Destination:=sh.Rows(s2row + 1)
                 s2row = s2row + 1
              End If
          Next
        Next
    End If
End If

End Sub




Ok great.This is creating a new sheet even when i delete a content in the excel
Can you change the case sensivity
Private Sub Worksheet_Change(ByVal Target As Range)
Static s2row
s2row = 1
Dim sh As Worksheet
Set sh = ActiveWorkbook.Sheets.Add(, ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count), , xlWorksheet)
If Target <> "" Then
   If Target.Column = 2 Then
        For i = 1 To Sheet1.UsedRange.Rows.Count
          For j = 1 To Sheet1.UsedRange.Columns.Count
              If UCase(Sheet1.Cells(i, j).Value) = UCase(Target.Value) Then
                 Sheet1.Rows(i).Copy Destination:=sh.Rows(s2row + 1)
                 s2row = s2row + 1
              End If
          Next
        Next
    End If
End If

End Sub

What about the deleting
After i enter fdd and then delete the content.It creates a blank sheet.
ASKER CERTIFIED SOLUTION
Avatar of Hitesh Manglani
Hitesh Manglani
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial