Frustrating Boolean/loop iteration problem

C

carg1

Hello all, I'm having a problem with a loop. I have two columns (C and
D but I used activecell just to generalize) that contain dates. What I
need the loop to do is compare the pairs of dates in C and D, and
replace the date in C if the two dates are different. If D2 is blank
or the if the two dates are the same, it should just skip on to the
next pair of dates.

It seemed simple enough, but then the logic failed. If the first pair
of cells it evaluates don't match (cn2 = true) and the cell in column d
isn't blank (cn1 = true), then it works fine and sets cn3 to true. For
every iteration after the first, it sets cn2 to false regardless of
whether or not they match. It evaluates cn1 without a problem on each
iteration, i.e., if the cell in column d is empty then it has no
problem. It just seems to go "blind" every time after the first with
regards to matching dates. I tried debugging to see if for some reason
it was setting the dates to be the same each time, but it reads them
fine. Can anybody lend a hand on this? Code below:


Code:
--------------------
Sub test()

Dim cn1 As Boolean, cn2 As Boolean, cn3 As Boolean

cn1 = ActiveCell.Offset(0, 1).Value <> Empty
cn2 = ActiveCell.Formula <> ActiveCell.Offset(0, 1).Formula
cn3 = ((cn1 = True) And (cn2 = True))

Do

Debug.Print ActiveCell.Value
Debug.Print ActiveCell.Offset(0, 1).Value

If cn3 = True Then
ActiveCell.Formula = ActiveCell.Offset(0, 1).Formula
End If
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Offset(0, 1).Formula = "STOP"

End Sub
 
M

microsoft.public.excel.programming

Carg1,

Try this ... (watch for word-wrap, suppose to be NONE)...


Sub test()
Range("c1").Select

Dim cn1 As Boolean, cn2 As Boolean

Do Until ActiveCell.Formula = Empty

cn1 = ActiveCell.Offset(0, 1).Value <> Empty
cn2 = ActiveCell.Formula <> ActiveCell.Offset(0, 1).Formula

If cn1 Then
If cn2 Then ActiveCell.Formula = ActiveCell.Offset(0, 1).Formula
End If
ActiveCell.Offset(1, 0).Select
Loop

End Sub


JimP

*** Sent via Developersdex http://www.developersdex.com ***
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top