1
votes

What I want to achieve is to copy data from WS1 to WS3 based on certain criteria.

I have 2 worksheets:

WS1 = RAW DATA  
WS2 = ATLAS DATA

In columns A of both there are unique identifiers. What I want to do is to create WS3=Reconciliation. Then look up values in WS2 against WS1. Where a match is found I want to copy row(s) from WS1 to WS3 that all I have reverse engineered some code and came up with one below

Sub CopyAndPaste()
Dim x As String, CpyRng As Range
Dim mFIND As Range, mFIRST As Range

    With Sheets("RAW DATA")
        Range("A:A").Select
        On Error Resume Next
End With
With Sheets("ATLAS DATA")
        Set mFIND = .Range("A:A").Find(x, LookIn:=xlValues, LookAt:=xlWhole)
        If Not mFIND Is Nothing Then
            Set CpyRng = mFIND
            Set mFIRST = mFIND

            Do
                Set CpyRng = Union(CpyRng, mFIND)
                Set mFIND = .Range("A:A").FindNext(mFIND)
            Loop Until mFIND.Address = mFIRST.Address

            CpyRng.EntireRow.Copy Sheets("Rec").Range("A" & Rows.Count).End(xlUp).Offset(1)
        End If
    End With
End Sub
1
Need help to make my code work I thought I had said earlier. I apologise if i didn't.Werra2006

1 Answers

0
votes

Based on your description of your problem; try this

Option Explicit

Sub CopyAndPaste()
Application.ScreenUpdating = False

    Dim i As Long, j As Long, lastRow1 As Long, lastRow2 As Long, cnt As Long
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Set ws1 = ActiveWorkbook.Sheets("RAW DATA")
    Set ws2 = ActiveWorkbook.Sheets("ATLAS DATA")
    Set ws3 = ActiveWorkbook.Sheets("Reconciliation")

    lastRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
    lastRow2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
    cnt = 1

    For i = 1 To lastRow1
        For j = 1 To lastRow2
            If StrComp(CStr(ws2.Range("A" & j).Value), _
                       CStr(ws1.Range("A" & i).Value), _
                       vbTextCompare) = 0 Then
                        ws1.Activate
                        ws1.Rows(i).Select
                        Selection.Copy
                        ws3.Activate
                        ws3.Range("A" & cnt).Select
                        Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme
                        Application.CutCopyMode = False
                        cnt = cnt + 1
            End If
        Next j
    Next i
Application.ScreenUpdating = True
End Sub