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

Board Regular
Joined
Dec 12, 2018
Messages
64
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: 18

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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
 
Upvote 0
Solution
Hi Yongle,

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

Regards

Craig
 
Upvote 0

Forum statistics

Threads
1,213,559
Messages
6,114,302
Members
448,564
Latest member
ED38

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