Re: For Next Loop
Here is my go at your issue. This does not have the part where it would recognise if it wasn't able to make the two values equal, but it may be a start in the right direction. It does increase or decrease A1 one year at a time depending on the beginning relationship between the two. I didn't have time to add the last bit as I had my own code dilemma today.
[code]
Sub changedate()
Dim rng1 As Range, rng2 As Range
Dim date1 As String
Dim date2 As String
Dim mth As String
Dim dy As String
Dim i As Integer
recheck:
Set rng1 = Worksheets("Sheet1").Range("a1")
Set rng2 = Worksheets("Sheet1").Range("b1")
'check if a1 is date value
If IsDate(rng1) And IsDate(rng2) Then
'set variables for comparison and month and day of a1 for change in value
date1 = Year(rng1)
mth = Month(rng1)
dy = Day(rng1)
date2 = Year(rng2)
'compare a1&b1
Else: Exit Sub
End If
If date1 = date2 Then Exit Sub
If date1 > date2 Then
date1 = date1 - 1
Cells(1, 1) = DateSerial(date1, mth, dy)
GoTo recheck
Else
If date2 > date1 Then
date1 = date1 + 1
Cells(1, 1) = DateSerial(date1, mth, dy)
GoTo recheck ' start at top and redo until a1 and b1 year value are equal
End If
End If
End Sub