do while loop problem

A

April

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
 
J

Jacob Skaria

Sorry I am unable to understand your code...but it is definitely missing the
below

END IF
Next


If this post helps click Yes
 
D

Don Guillett

I don't understand it either, especially the part about range(am1) but try

Sub loopthrough()
Dim C As Range
for each c in range("t618:t1387")
If C.Value = "Karlan-Gine" and Range("AM1").Value > 0 Then
C.Offset(0, 15).Value = "WBK0000"
C.Offset(0, 16).Value = C.Offset(0, 6)
End If
End Sub
 
R

Ron Rosenfeld

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

You have two IF statements but only one END IF

VBA gives that kind of error message in that circumstance.
--ron
 
R

Rick Rothstein

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
 

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