I have wrote macro for fun (I have just started learning VBA) to loop through a list of names in a column in sheet1, and if the name matches to a similliar list in sheet2, then paste the rest of the data in sheet2. But it keeps me giving an application error, although I have checked my code a countless of time I am pretty sure is some dumb mistake but I am unable to find it.
Option Explicit
Sub RangePasteColumn()
Dim j As Long, i As Long, lastRow1 As Long, lastRow2 As Long
Dim MyName As String
Sheets("sheet1").Activate
lastRow1 = Sheets("sheet1").Range("E" & Rows.Count).End(xlUp).Row
For j = 4 To lastRow1
MyName = Sheets("sheet1").Cells(j, "E").Value
Sheets("sheet3").Activate
lastRow2 = Sheets("sheet3").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow2
If Sheets("sheet3").Cells(i, "A").Value = MyName Then
Sheets("sheet1").Activate
Sheets("sheet1").Range(Cells(j, "F"), Cells(j, "I")).Copy
Sheets("sheet3").Activate
Sheets("sheet3").Range(Cells(i, "B"), Cells(i, "E")).Select
ActiveSheet.Paste
End If
Next i
Application.CutCopyMode = False
Next j
Sheets("sheet3").Activate
Sheets("sheet3").Range("A1").Select
End Sub
I know you can do a simple vlookup or index/match function for this task, I was just doing for a sake of learning not for work. Hope u guys can guide me here.
yea, one more thing, I was wondering if i can use the offset in my vba code, rather then writing which range to copy. if you know please let me know.
thanks