How to use datediff properly and take action according to the difference.

fahadun

New Member
Joined
Jul 27, 2017
Messages
22
Hope everybody is fine.
I am stuck with a problem. what i am trying to do is subtract dates of column C from column AQ dates.
the logic is

if aq - c <= 20 then
copy entire row to sheet1
else copy entire row to sheet2.

column c contains empty cells it should automatically go to sheet 2 right?
if not i want to copy all the empty cell on column c to sheet2.
the problem i am facing is its copying all the rows to the sheet1, in different levels. nothing is going to the sheet2.
the code is here.

Code:
Sub tesst()

Dim RowNo As Long
Dim FirstDate, SecondDate As Date
Dim ws, ws1, ws2 As Worksheet


Set ws = ThisWorkbook.Sheets("Updated_Date_Exceeded_data")
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")

RowNo = 2

    Do Until ws.Cells(RowNo, 1) = ""

    FirstDate = ws.Cells(RowNo, 43)
    SecondDate = ws.Cells(RowNo, 3)
    
    If DateDiff("d", FirstDate, SecondDate) <= 20 Then
        ws.Cells(RowNo, 1).EntireRow.Copy Destination:=ws1.Cells(RowNo, 1)
        Else: ws.Cells(RowNo, 1).EntireRow.Copy Destination:=ws2.Cells(RowNo, 1)
    End If

    RowNo = RowNo + 1

    Loop
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Does the below work better (if you decide you don't want the empty cells copied to either sheet then you want the commented out code).

Please note that have gone by your description of "if aq - c <= 20 then " rather than your code.

Rich (BB code):
Sub tesst()

    Dim RowNo As Long
    Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet

    Application.ScreenUpdating = False

    Set ws = ThisWorkbook.Sheets("Updated_Date_Exceeded_data")
    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Set ws2 = ThisWorkbook.Sheets("Sheet2")

    With ws

        For RowNo = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
            If CLng(.Cells(RowNo, 43)) - CLng(.Cells(RowNo, 3)) <= 20 Then
                .Cells(RowNo, 1).EntireRow.Copy ws1.Cells(RowNo, 1)
                '.Cells(RowNo, 1).EntireRow.Copy ws1.Cells(Rows.count, "A").End(xlUp).offset(1)
            Else
                .Cells(RowNo, 1).EntireRow.Copy ws2.Cells(RowNo, 1)
                '.Cells(RowNo, 1).EntireRow.Copy ws2.Cells(Rows.count, "A").End(xlUp).offset(1)
            End If
        Next

    End With

    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Sorry too late to edit the previous post but

if you decide you don't want the empty cells copied to either sheet then you want the commented out code

should read as

if you decide you want the data and empty cells copied to next row and not RowNo then you want the commented out code.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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