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
32
Office Version
  1. 2019
Platform
  1. Windows
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: 11

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
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

Craig_Moore

New Member
Joined
Dec 12, 2018
Messages
32
Office Version
  1. 2019
Platform
  1. Windows
Hi Yongle,

thanks for the Help this has worked great for what i need

Regards

Craig
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,504
Messages
5,832,106
Members
430,111
Latest member
Francis Xavier

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
Top