Compare Two Dates and if No difference then copy Specific Cell value and paste to another sheet

Gogo007

New Member
Joined
Feb 22, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have sheet1 in that I have a column N "Due Date" {Date format} and Column V "Invoice Due Date" {Date format}, if both the values are the same then copy the Amount from Column X from that row and Paste to Sheet2.

I request need a VBA code for this.
I have attached the snap for better understanding.

Please if anyone can help me out.
Compare Two dates.png
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Gogo,

Try this
SQL:
Sub Copy_to_Sheet2()
    Sheets("Sheet1").Select
    Sheets("Sheet1").Range(Range("N65000").End(xlUp), Range("N65000").End(xlUp).End(xlUp).Offset(1)).Name = "DueDate_Rng"
    Dim SGcell As Range
    For Each SGcell In Range("DueDate_Rng")
        If SGcell.Value = SGcell.Offset(0, 8).Value Then
            SGcell.Offset(0, 11).Value = "V"
            Sheets("Sheet2").Range("A65000").End(xlUp).Offset(1).Value = SGcell.Offset(0, 8).Value
            Sheets("Sheet2").Range("B65000").End(xlUp).Offset(1).Value = SGcell.Offset(0, 10).Value
        Else
            SGcell.Offset(0, 11).Value = "X"
        End If
    Next
End Sub

By the way, your file should be saved as Excel Macro-Enabled Workbook (*.xlsm)
 
Upvote 0
Hi Gogo,

Try this
SQL:
Sub Copy_to_Sheet2()
    Sheets("Sheet1").Select
    Sheets("Sheet1").Range(Range("N65000").End(xlUp), Range("N65000").End(xlUp).End(xlUp).Offset(1)).Name = "DueDate_Rng"
    Dim SGcell As Range
    For Each SGcell In Range("DueDate_Rng")
        If SGcell.Value = SGcell.Offset(0, 8).Value Then
            SGcell.Offset(0, 11).Value = "V"
            Sheets("Sheet2").Range("A65000").End(xlUp).Offset(1).Value = SGcell.Offset(0, 8).Value
            Sheets("Sheet2").Range("B65000").End(xlUp).Offset(1).Value = SGcell.Offset(0, 10).Value
        Else
            SGcell.Offset(0, 11).Value = "X"
        End If
    Next
End Sub

By the way, your file should be saved as Excel Macro-Enabled Workbook (*.xlsm)
Thanks for the input.
Just to make sure
Sheet1 in Column N has 1st date and Sheet1 in Column U has 2nd date and to compare and copy the value from Sheet1 in Column X paste to Sheet2 from D4 to down...d5,d6,d7,d8 (if date matches from Sheet1).

Many Thanks.
 
Upvote 0
Hi Gogo,

This VBA code does:

  • Assigns name "DueDate_Rng" to range N9:N12
  • Loops “"DueDate_Rng" & comparing (matching) dates in columns “N” & “V
  • If there is a match,
    • letter “V” will be entered on the same row in column “Y”
    • The date & amount from column “X” will be copied in “Sheet2”, columns “A” & “B
If no match, the letter “X” will be entered on the same row in column “Y
 
Upvote 0
Hi Gogo,

This VBA code does:

  • Assigns name "DueDate_Rng" to range N9:N12
  • Loops “"DueDate_Rng" & comparing (matching) dates in columns “N” & “V
  • If there is a match,
    • letter “V” will be entered on the same row in column “Y”
    • The date & amount from column “X” will be copied in “Sheet2”, columns “A” & “B
If no match, the letter “X” will be entered on the same row in column “Y
Hi Sahak,

Thanks for the guidance,
It's working fine after changing some row no.
Need one suggestion, I am applying this code on filtered row (visible cells), but this code is running for all the cells, so can you please help me with that.
The code should consider only visible cells.

Thanks
 
Upvote 0
Hi Gogo,

I think this is what you need
VBA Code:
Sub Copy_Visible_to_Sheet2()
    Sheets("Sheet1").Select
    Sheets("Sheet1").Range(Range("N65000").End(xlUp), Range("N65000").End(xlUp).End(xlUp).Offset(1)).Name = "DueDate_Rng"
    Dim SGcell As Range
    For Each SGcell In Range("DueDate_Rng")
        Dim RowNo As Long
        RowNo = SGcell.Row
        If Not ActiveSheet.Rows(RowNo).EntireRow.Hidden Then
            If SGcell.Value = SGcell.Offset(0, 8).Value Then
                SGcell.Offset(0, 11).Value = "V"
                Sheets("Sheet2").Range("A65000").End(xlUp).Offset(1).Value = SGcell.Offset(0, 8).Value
                Sheets("Sheet2").Range("B65000").End(xlUp).Offset(1).Value = SGcell.Offset(0, 10).Value
            Else
                SGcell.Offset(0, 11).Value = "X"
            End If
        End If
    Next
End Sub
 
Upvote 0
Solution
Hi Gogo, did you try the last code I sent?
 
Upvote 0
You are very welcome. Glad to be helpful.
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,527
Members
449,037
Latest member
tmmotairi

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