Working with a Loop

R

R Tanner

I can't figure out why this double loop does not work...

For X = 1 To 9
For T = 1 To 11
range("SP[DATE]").Select
Selection.Find(What:=CURRPERCENTAGE.Value, After:=ActiveCell,
LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(0,
1).Activate
CURRPERCENTAGE.Offset(X, 0) = ActiveCell
Set CURRPERCENTAGE = CURRPERCENTAGE.Offset(0, 1)
T = T + 1
MsgBox T
Next

Next

If you have any idea, it would be greatly appreciated..I have tried
everything...
 
S

smartin

Elmtree said:
I see where you increment T .... T = T + 1

But not for X ???

R said:
I can't figure out why this double loop does not work...

For X = 1 To 9
For T = 1 To 11
range("SP[DATE]").Select
Selection.Find(What:=CURRPERCENTAGE.Value, After:=ActiveCell,
LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(0,
1).Activate
CURRPERCENTAGE.Offset(X, 0) = ActiveCell
Set CURRPERCENTAGE = CURRPERCENTAGE.Offset(0, 1)
T = T + 1
MsgBox T
Next

Next

If you have any idea, it would be greatly appreciated..I have tried
everything...

Both T and X are incremented in the for/next loops.

The thing that caught my eye is T is also incremented by T = T + 1
/inside/ the T loop. This amounts to evaluating T as 1, 3, 5, etc.,
although the MsgBox will report 2, 4, 6, etc. I don't know what the OP
is trying to do here, but this seems odd to me.
 
R

Rick Rothstein

It would have helped if you had told us what "does not work" meant (did you
see an error message, which one, nothing happened, etc.). I do note that you
have T=T+1 in your code... perhaps that statement is your problem as it
would force T to increment by two on each loop (once for that statement and
once more from the For statement itself. Comment that statement out and see
if that corrects your problem.
 
E

Elmtree

I copied the code and tried it myself.

I removed the T = T + 1 line. also removed the code inside the T loop.

It increments by 1 now, but when it finishes with T, it repeats the T loop.


That may be his problem.




Rick said:
It would have helped if you had told us what "does not work" meant (did you
see an error message, which one, nothing happened, etc.). I do note that you
have T=T+1 in your code... perhaps that statement is your problem as it
would force T to increment by two on each loop (once for that statement and
once more from the For statement itself. Comment that statement out and see
if that corrects your problem.
I can't figure out why this double loop does not work...
[quoted text clipped - 17 lines]
If you have any idea, it would be greatly appreciated..I have tried
everything...
 
E

Elmtree

Let me clairfy my last statement.

I think it works OK once you take the T = T + 1 line out.

What he sees is the T msgbox repeating itself. But what he does not see is
the X value.

I put in a X msgbox, it loops the T until X = 9, then ends.



Maybe this fixed his problem....

I copied the code and tried it myself.

I removed the T = T + 1 line. also removed the code inside the T loop.

It increments by 1 now, but when it finishes with T, it repeats the T loop.

That may be his problem.
It would have helped if you had told us what "does not work" meant (did you
see an error message, which one, nothing happened, etc.). I do note that you
[quoted text clipped - 8 lines]
 
R

Rick Rothstein

I didn't try his code... I just am pretty sure the T=T+1 is not right (if
the OP really needed this, he would simply have to use a Step 2 qualifier on
his For statement line).

--
Rick (MVP - Excel)


Elmtree said:
I copied the code and tried it myself.

I removed the T = T + 1 line. also removed the code inside the T loop.

It increments by 1 now, but when it finishes with T, it repeats the T
loop.


That may be his problem.




Rick said:
It would have helped if you had told us what "does not work" meant (did
you
see an error message, which one, nothing happened, etc.). I do note that
you
have T=T+1 in your code... perhaps that statement is your problem as it
would force T to increment by two on each loop (once for that statement
and
once more from the For statement itself. Comment that statement out and
see
if that corrects your problem.
I can't figure out why this double loop does not work...
[quoted text clipped - 17 lines]
If you have any idea, it would be greatly appreciated..I have tried
everything...
 
D

Don Guillett

It may be best to start from the beginning and tell us your layout and what
you want to happen.
I can't see T doing anything and selections are not necessary.

If desired, send your workbook to my address below along with an explanation
and I'll take a look.
 

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