vba code that will check if date is already present if it is a pop up asks if im sure i want to copy data

Craig_Moore

New Member
Joined
Dec 12, 2018
Messages
21
Hi i am currently using the below code to copy select ranges from one workbook to paste into another workbook, but when data is being updated by different departments and resent im getting duplicate data and i am having to go in to the document DOWN TIME SHEET PNB 2020 .xlsx to clear the duplicate data.

so i am after some modification to the below code that will check if there is already a column with the same date that is being copied over if there is, then a popup box asks do you want to update the existing data if yes the data over rights that column that has the existing data if no the data is copied to the next free column.

Thanks

Craig

VBA Code:
Private Sub CommandButton2_Click()
Workbooks.Open "\\duerrsfile\SS\01 PNB Production\OEE SHEETS\02. DOWN TIME SHEET\DOWN TIME SHEET PNB 2020 .xlsx"
 Application.ScreenUpdating = False
    Dim copySheet As Worksheet, pasteSheet As Worksheet, lCol As Long


' LINE 3 COPY PASE CODE,
    
    Set copySheet = ThisWorkbook.Sheets("OVERVIEW")
    Set pasteSheet = Workbooks("DOWN TIME SHEET PNB 2020 .xlsx").Sheets("YTD DOWNTIME")
    lCol = pasteSheet.Cells(4, pasteSheet.Columns.Count).End(xlToLeft).Column + 1 ' CHANGE THE 5 UP OR DOWN TO SELECT WHICH ROW TO START THE PASTE FROM
    copySheet.Range("LINE_3_DT").Copy ' CHANGE THIS RANGE IF THE RANGE NEEDS TO BE MADE BIGGER OR SMALLER
    pasteSheet.Cells(4, lCol).PasteSpecial xlPasteValues ' THE 5 VALUE MUST MATCH THE VALUE 2 LINES OF CODE UP
    Application.CutCopyMode = False
    Application.ScreenUpdating = True


Close Workbook

ActiveWorkbook.Close Savechanges:=True
 

Attachments

  • down time.png
    down time.png
    115.6 KB · Views: 6

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,872
Office Version
  1. 365
Platform
  1. Windows
Try this (untested)

VBA Code:
Private Sub CommandButton2_Click()
    Workbooks.Open "\\duerrsfile\SS\01 PNB Production\OEE SHEETS\02. DOWN TIME SHEET\DOWN TIME SHEET PNB 2020 .xlsx"
    Application.ScreenUpdating = False
    Dim copySheet As Worksheet, pasteSheet As Worksheet, lCol As Long


' LINE 3 COPY PASE CODE,
    
    Set copySheet = ThisWorkbook.Sheets("OVERVIEW")
    Set pasteSheet = Workbooks("DOWN TIME SHEET PNB 2020 .xlsx").Sheets("YTD DOWNTIME")
    
    On Error Resume Next
    lCol = WorksheetFunction.Match(copySheet.Range("LINE_3_DT").Cells(1, 1), pasteSheet.Range("4:4"), 0)
    On Error GoTo 0
    If lCol > 0 Then
        If MsgBox("Duplicate date - overwrite ?", vbYesNo, "") <> vbYes Then GoTo CloseAndSave
    Else
        lCol = pasteSheet.Cells(4, pasteSheet.Columns.Count).End(xlToLeft).Column + 1
    End If
    
    copySheet.Range("LINE_3_DT").Copy
    pasteSheet.Cells(4, lCol).PasteSpecial xlPasteValues ' THE 5 VALUE MUST MATCH THE VALUE 2 LINES OF CODE UP
    Application.CutCopyMode = False
    Application.ScreenUpdating = True

'Close Workbook

CloseAndSave:
ActiveWorkbook.Close Savechanges:=True

End Sub
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,118,076
Messages
5,570,070
Members
412,310
Latest member
mark884
Top