Compare two columns and populate the third

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hi,

<p> I have two dates, col B and col AY. I need to populate the value in col BB as 1, if value of col AY is equal or less than 30 days of col B.
If it is more than 30 days, it should do nothing as shown in row 3 and 4.
TSS.png


<code>

firstDate = ws.Range("B" & Rows.Count).End(xlUp).Row
secondDate = ws.Range("AY" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If DateDiff("d", firstDate, secondDate) <= 30 Then
ws.Range("BB" & i).Value = "1"
End If
Next i

</code>

It is putting all values in BB as 1. I think i need the code to read the dates through all the rows in col B and col AY .
I need help with my code </p>

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this
VBA Code:
Sub DateTest()

Dim i As Long, LastRow As Long
Dim FirstDate As Date, SecondDate As Date
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")    ' Change sheet name accordingly

LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row

For i = 2 To LastRow
    FirstDate = ws.Range("B" & i)
    SecondDate = ws.Range("AY" & i)
    If DateDiff("d", FirstDate, SecondDate) <= 30 Then ws.Range("BB" & i) = 1
Next

End Sub
 
Upvote 0
Solution
Try this
VBA Code:
Sub DateTest()

Dim i As Long, LastRow As Long
Dim FirstDate As Date, SecondDate As Date
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")    ' Change sheet name accordingly

LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row

For i = 2 To LastRow
    FirstDate = ws.Range("B" & i)
    SecondDate = ws.Range("AY" & i)
    If DateDiff("d", FirstDate, SecondDate) <= 30 Then ws.Range("BB" & i) = 1
Next

End Sub
Worked perfectly, Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,605
Messages
6,120,473
Members
448,967
Latest member
visheshkotha

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