PDA

View Full Version : Copy cells from Sheet2, paste to next blank row in Sheet1



marreco
02-11-2013, 03:21 AM
Hi.
the code below, demand data "Sheet1" with discretion in "Sheet2"
to find it overrides the value of "Sheet2" and put in "Sheet1"

Option Explicit

Sub Macro1()

Dim rngCell As Range
Dim lngMatchRowNum As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")
For Each rngCell In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
If (IsError(Evaluate("VLOOKUP(" & Sheets("Sheet1").Range(rngCell.Address(False, False)) & ",Sheet2!A:A,1,FALSE)"))) = False Then
lngMatchRowNum = Evaluate("MATCH(" & Sheets("Sheet1").Range(rngCell.Address(False, False)) & ",Sheet2!A:A,0)")
.Range("B" & rngCell.Row & ":F" & rngCell.Row).Value = Sheets("Sheet2").Range("B" & lngMatchRowNum & ":F" & lngMatchRowNum).Value
End If
Next rngCell
End With
Application.ScreenUpdating = True
MsgBox "Todos os registros aplicáveis já foram atualizados."

End Sub
Now what I need is:
If the value of "Sheet2" not found in "Sheet2" so that the value
the "Sheet2" is inderido the next empty line (below) of "Sheet1"

Admin
02-11-2013, 07:00 PM
Hi

Try


Option Explicit

Sub kTest()

Dim x, K1 As Range, K2, i As Long, r As Long

Set K1 = Sheet1.Range("a1").CurrentRegion.Columns(1)
K2 = Sheet2.Range("a1").CurrentRegion.Value2

r = K1.Rows.Count
For i = 2 To UBound(K2, 1)
x = Application.Match(K2(i, 1), K1.Value2, 0)
If IsError(x) Then
K1.Cells(1).Offset(r).Resize(, UBound(K2, 2)) = Application.Index(K2, i, 0)
r = r + 1
End If
Next

End Sub

marreco
02-11-2013, 08:13 PM
Hi.

Was grerat!!

Thank you!!