Thread: Copy data
View Single Post
 
Old 02-25-2019, 12:46 PM
NoSparks NoSparks is offline Windows 7 64bit Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Code:
Sub CopyOwnTab_2()

Dim i As Long, Lastrow As Long, ans As String
Dim dest As Worksheet, recCnt As Long
Dim fndRng As Range, findString As String

With Sheets("List")
    Lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
    For i = 3 To Lastrow
        ans = .Cells(i, "B").Value          'the customer
        findString = .Cells(i, "C").Value   'the invoice
        On Error Resume Next        'incase the sheet does not exist
        Set dest = Sheets(ans)
        On Error GoTo 0             're-enable error notification
        
        If Not dest Is Nothing Then 'the customer sheet does exist
                'so check if this invoice is already there
                Set fndRng = dest.Range("C:C").Find(What:=findString, LookIn:=xlValues, _
                                                    LookAt:=xlWhole, SearchOrder:=xlByRows, _
                                                    SearchDirection:=xlNext, MatchCase:=False)
                If fndRng Is Nothing Then   'the invoice does not exist
                    .Cells(i, "A").Resize(, 7).Copy dest.Range("A" & Rows.Count).End(xlUp).Offset(1)
                    recCnt = recCnt + 1
                End If
        Else
            MsgBox "Sheet " & ans & " does not exist."
        End If
        Set dest = Nothing
    Next i
End With
    MsgBox "There were " & recCnt & " records copied."
End Sub
Reply With Quote