View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default do while loop problem

Don't be thrown by the exact wording of that error message... VB will
generate a "without Do", "without Next", "without End If", etc. whenever one
or more "blocking" type structures are not complete. By blocking structure I
mean code groupings that require a start and end statement such as
If..End.If, Do..Loop, For..Next, etc. VB treats all blocks the same (sort
of) so that when a closing block statement is missing where several blocking
structures are nested within each other, it can't fully trace the logic, so
it "guesses" at which closing statement might be missing... sometimes it
gets it right, more often than not it doesn't. So don't concentrate on the
exact wording rather than the indication that one or more blocking
structures are not closed. In your case, there are two closing statements
missing... a Next statement to close of the For..Next block you started
(that statement goes right before the End Sub statement as your code is
constructed... however, the End If statement is harder to place because I
don't fully understand what your code is intending to do. I have marked two
positions in your code where the End If looks like it would properly go...
only **one** of them is correct and you should remove the other one. By the
way, if you indent your code for each blocking structure you start and
close, it will be easier to see when something is missing.

Sub loopthrough()
Dim C As Range
Dim MyRange As Range
Dim myRow As Integer

myRow = 1
Set MyRange = Range("t618:t1387")
For Each C In MyRange
Do Until myRow = 770
If C.Value = "Karlan-Gine" Then
If Range("AM1").Value 0 Then
C.Offset(0, 15).Value = "WBK0000"
C.Offset(0, 16).Value = C.Offset(0, 6)
End If
== End If
ActiveCell.Offset(1, 0).Select
== End If
myRow = myRow + 1
Loop
Next
End Sub

--
Rick (MVP - Excel)


"April" wrote in message
...
i have added a do while instruction to my macro

Sub loopthrough()
Dim C As Range
Dim MyRange As Range

Dim myRow As Integer
myRow = 1

Set MyRange = Range("t618:t1387")
For Each C In MyRange
Do Until myRow = 770
If C.Value = "Karlan-Gine" Then
If Range("AM1").Value 0 Then
C.Offset(0, 15).Value = "WBK0000"
C.Offset(0, 16).Value = C.Offset(0, 6)

End If

ActiveCell.Offset(1, 0).Select

myRow = myRow + 1
Loop
End Sub
-- and get this error message "Compile error.Loop without Do.

i have a Do statement at the beginning and don't see what is the problem.

thanks in advance
BDW