check all sheets for duplicate data before copying to sheets with no fixed name

brankscaffold

New Member
Joined
Jun 15, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
This code works well.
copy and create new sheet with name and year. this is good as it should be.

but if I run this code again after adding new data I get all duplicates
a check must also be carried out on the existing sheets before they are copied to this sheet.

Can anyone help me with this code.
because I can't figure it out


VBA Code:
Sub Inboekingen_kopieren()
   
    Const FirstRow = 5
    Dim rij As Long
    Dim n As Long
    Dim src As Worksheet
    Dim trg As Worksheet
    Dim OKToCopy As String
    




    Set src = Sheets("Jaar 2022")

    Application.ScreenUpdating = False
   
    For n = FirstRow To src.Cells(Rows.Count, "AE").End(xlUp).Row
        OKToCopy = "N"
        Set trg = Nothing
        If src.Cells(n, "AE").Value <> "" Then
           If n = FirstRow Or _
            src.Cells(n, "B").Value <> src.Cells(n - 1, "B").Value Or _
            src.Cells(n, "E").Value <> src.Cells(n - 1, "E").Value Or _
            src.Cells(n, "H").Value <> src.Cells(n - 1, "H").Value Or _
            src.Cells(n, "AD").Value <> src.Cells(n - 1, "AD").Value Then
            
            
    OKToCopy = "Y"
Else
    OKToCopy = "N"
End If
           
           If OKToCopy = "Y" Then
                    On Error Resume Next
                    Set trg = Worksheets(src.Cells(n, "AE").Value & " 22")
                    If Err <> 0 Then
                        Err.Clear
                        Set trg = ActiveWorkbook.Sheets.Add(After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count))
                        trg.Name = src.Cells(n, "AE").Value & " 22"
                        With src
                            .Range(.Cells(1, "A"), .Cells(4, "AZ")).Copy
                        End With
                        trg.Cells(1, "A").PasteSpecial
                    End If
                    rij = trg.Cells(Rows.Count, "AE").End(xlUp).Row + 1
                    With src
                        .Range(.Cells(n, "A"), .Cells(n, "AZ")).Copy
                    End With
                    trg.Cells(rij, "A").PasteSpecial
            End If
        End If
    Next n
    On Error GoTo 0
   
    Application.ScreenUpdating = True
   
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
or the sheet where the data goes to must first be checked to see if this date is already there with the same search conditions as have already been used for selection copy I don't know what is best and certainly don't know which designation/code I need to use
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top