View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Patti[_5_] Patti[_5_] is offline
external usenet poster
 
Posts: 20
Default One more loop question

Hi Bob,

You are right that this now evaluates every row...but now it won't find the
flag for each name unless it appears in the same row as the first instance
of the name.

So if I have in column A: Column L:

Smith blah
Smith This '( my flag)

I want column P of the *first* row where Smith occurs to say "I found This!"
The use of the oCurrent variable was actually a tip I got from you, to
capture the row where I found the first instance of a new name. And of
course it works great.

(sorry if I broke any rules of netiquette by staring a new thread...nearly a
week has passed since I last posted & I thought it unlikely that the last
would be revisited).

Anyway, I just don't understand the "why" behind it skipping a row on the
change of name. As long as I'm stuck with this problem, I am trying to
make it a good learning experience...

I really appreciate all the help you given me.

Patti






"Bob Phillips" wrote in message
...
Patti,

You skip a row on change of name rather than process it. I've tested it
before the Do Loop, and removed the Else.

Also, you don't need the oCurrent variable, you can use the index counter

Public Sub testloop()

Dim LstRow As Long
Dim i As Long
Dim AgtName As String
Dim FoundFlag As Boolean

LstRow = Range("A" & Rows.Count).End(xlUp).Row
AgtName = Range("A1").Value
FoundFlag = False

For i = 1 To LstRow

With Cells(i, "A")
If .Value < AgtName Then
AgtName = .Value
FoundFlag = False
End If
Do While FoundFlag = False
Select Case .Cells(1, 12).Value
Case ""
.Offset(0, 15).Value = "I found a Null cell"
FoundFlag = True
Case "This"
.Offset(0, 15).Value = "I found This!"
FoundFlag = True
Case "That"
.Offset(0, 15).Value = "I found That!"
FoundFlag = True
Case Else
FoundFlag = True
End Select
Loop
' message box added to confirm which row is being evaluated
MsgBox "Currently evaluating row " & i & " for agent " &

AgtName
End With
Next i

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Patti" wrote in message
...
Can someone please explain to me why the following code is skipping each

row
in which the *first* instance of a new name appears in column A? In

other
words, the Case Statement starts evaluating on the 2nd occurrence of the
name.

(thanks Bob, for all your help so far...)

TIA,

Patti


Option Explicit
Private Sub testloop()

Dim LstRow As Long
Dim i As Long
Dim AgtName As String
Dim oCurrent As Range
Dim FoundFlag As Boolean

LstRow = Range("a" & Rows.Count).End(xlUp).Row
AgtName = Range("A1").Value
Set oCurrent = Range("A1")
FoundFlag = False

For i = 1 To LstRow

If Cells(i, "A") = AgtName Then
Do While FoundFlag = False
Select Case Cells(i, 12)
Case ""
oCurrent.Offset(0, 15).Value = "I found a

Null
cell"
FoundFlag = True
Case "This"
oCurrent.Offset(0, 15).Value = "I found

This!"
FoundFlag = True
Case "That"
oCurrent.Offset(0, 15).Value = "I found

That!"
FoundFlag = True
Case Else
FoundFlag = True
End Select
Loop
' message box added to confirm which row is being
evaluated
MsgBox "Currently evaluating row " & i & " for

agent
" & AgtName
Else
AgtName = Cells(i, "A").Value
Set oCurrent = Cells(i, "A")
FoundFlag = False
End If
Next i

End Sub