Hey guys, hope someone can help me.
I'm trying to write vba code to copy a cell from sheet to sheet.
I have a case where I want excel to find my typed word in cell A5 (Sheet1) on sheet2 of column 1. Then copy the row of the found word or cell next to the found word.
I wrote the code, but it doesn't work and I don't know why. This is my code:
I'm trying to write vba code to copy a cell from sheet to sheet.
I have a case where I want excel to find my typed word in cell A5 (Sheet1) on sheet2 of column 1. Then copy the row of the found word or cell next to the found word.
I wrote the code, but it doesn't work and I don't know why. This is my code:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim var, i As Variant
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
sheet2lastrow = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
If Target.Address = "$B$5" Then
On Error Resume Next
For i = 2 To sheet2lastrow
If ws1.Range("A5").Value = ws2.Range("A" & i).Value Then
sheet2.Range("B" & i).Value = ws1.Range("$B$5").Value
End If
Next i
If Err <> 0 Then Exit Sub
On Error GoTo 0
End If
End Sub