Use VBA to check if source date is present on target sheet before copying data

Doge Robert

New Member
Joined
Jan 10, 2017
Messages
14
Hey there.

I have a macro that copies data from one sheet to the first empty row of another... No problem there.
In row H of both datasets is a date. I would like my macro to check if the date in row H of the source sheet (all rows have the same date) is present somewhere in row H of the target sheet, BEFORE data is copied and if the date is present, stop the macro and make a pop up with the text "Data is already copied for specified date".

Any help is appreciated. :)

This is the macro, I'm currently using:

Sub CopyDataToHistory()

Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim CopyLastRow As Long
Dim DestLastRow As Long

'Set Variables
Set wsCopy = Sheets("TblHistoriskeData") 'Sourcedata sheetname
Set wsDest = Sheets("Historiske Data") 'Target sheetname
'Find Last Row in Copy Range
CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row

'Find 1st blank row in Destination Range - Offset 1 row
DestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row

'Copy & Paste Data
wsCopy.Range("A2:h" & CopyLastRow).Copy _
wsDest.Range("A" & DestLastRow)

End Sub

Edit: Typos
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi,
untested but see if this update to your code does what you want

VBA Code:
Sub CopyDataToHistory()
    Dim DateExists  As Boolean
    Dim wsCopy      As Worksheet, wsDest As Worksheet
    Dim CopyLastRow As Long, DestLastRow As Long
    
    'Set Variables
    Set wsCopy = ThisWorkbook.Worksheets("TblHistoriskeData")        'Sourcedata sheetname
    Set wsDest = ThisWorkbook.Worksheets("Historiske Data")        'Target sheetname
    
    'check if date exists in destination sheet column H
    DateExists = Not IsError(Application.Match(CLng(wsCopy.Cells(2, 8).Value), wsDest.Columns(8), 0))
    
    If Not DateExists Then
        'Find Last Row in Copy Range
        CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
        
        'Find 1st blank row in Destination Range - Offset 1 row
        DestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
        
        'Copy & Paste Data
        wsCopy.Range("A2:H" & CopyLastRow).Copy wsDest.Range("A" & DestLastRow)
        
    Else
    
        MsgBox "Data Is already copied For specified date", 48, "Date Exists"
        
    End If
    
End Sub

I have assumed dates in Column H are real dates

Dave
 
Upvote 0
Solution
Hi,
untested but see if this update to your code does what you want

VBA Code:
Sub CopyDataToHistory()
    Dim DateExists  As Boolean
    Dim wsCopy      As Worksheet, wsDest As Worksheet
    Dim CopyLastRow As Long, DestLastRow As Long
   
    'Set Variables
    Set wsCopy = ThisWorkbook.Worksheets("TblHistoriskeData")        'Sourcedata sheetname
    Set wsDest = ThisWorkbook.Worksheets("Historiske Data")        'Target sheetname
   
    'check if date exists in destination sheet column H
    DateExists = Not IsError(Application.Match(CLng(wsCopy.Cells(2, 8).Value), wsDest.Columns(8), 0))
   
    If Not DateExists Then
        'Find Last Row in Copy Range
        CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
       
        'Find 1st blank row in Destination Range - Offset 1 row
        DestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
       
        'Copy & Paste Data
        wsCopy.Range("A2:H" & CopyLastRow).Copy wsDest.Range("A" & DestLastRow)
       
    Else
   
        MsgBox "Data Is already copied For specified date", 48, "Date Exists"
       
    End If
   
End Sub

I have assumed dates in Column H are real dates

Dave
Thank you very much. It seems to be working exactly, as I envisioned. :-)
 
Upvote 0
most welcome glad suggestion does what you want & appreciate your feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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