Click here to Skip to main content
15,888,325 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

In a workbook there are 2 sheets, if i click or double click on a particular value present in column A, then that value should be searched in sheet 2 and after the match is found then sheet 2 should be opened and that particular cell where value is present should be highlighted.


This is what i have done. The code works only for 1st value but i guess some for loop issue because of which it is not working for other values. Code is:

Sub findvalue()
Dim a As String
Dim b As String
Dim someSheet As Worksheet
Dim i As Integer
Dim cell As Range

a = ActiveCell.Value


Dim LastRow As Long
With ActiveWorkbook.Sheets("Sheet2")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
MsgBox LastRow

For i = 2 To LastRow


b = Sheet2.Range("A" & i).Value

If a = b Then

Set someSheet = ActiveWorkbook.Sheets("Sheet2")
someSheet.Activate

ActiveWorkbook.Sheets("Sheet2").Activate

someSheet.Range("A" & i).Select

End If
Exit For

Next i

End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A2")) Is Nothing Then

findvalue
End If
End Sub


Regards,
Archies
Posted
Updated 13-Aug-14 21:39pm
v2
Comments
Maciej Los 12-Aug-14 17:10pm    
What have you done till now? Where are you stuck?
archies_gall 13-Aug-14 13:54pm    
This is what i have done. The code works only for 1st value but i guess some for loop issue because of which it is not working for other values. Code is:

Sub findvalue()
Dim a As String
Dim b As String
Dim someSheet As Worksheet
Dim i As Integer
Dim cell As Range

a = ActiveCell.Value


Dim LastRow As Long
With ActiveWorkbook.Sheets("Sheet2")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
MsgBox LastRow

For i = 2 To LastRow


b = Sheet2.Range("A" & i).Value

If a = b Then

Set someSheet = ActiveWorkbook.Sheets("Sheet2")
someSheet.Activate

ActiveWorkbook.Sheets("Sheet2").Activate

someSheet.Range("A" & i).Select

End If
Exit For

Next i

End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A2")) Is Nothing Then

findvalue
End If
End Sub
Maciej Los 13-Aug-14 16:37pm    
Do not post code in comment. Better use "Improve question" widget.
archies_gall 16-Aug-14 1:31am    
Any help on this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900