ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   For/Next with an If statement embedded (https://www.excelbanter.com/excel-programming/318082-next-if-statement-embedded.html)

Papa Jonah

For/Next with an If statement embedded
 
I have a condition that throws me into this loop. However I need to know how
to stop the loop per the If Then statement at the end.
Depending on how I have the ' marks, I either return to this sub-routine or
I get an error that says I have a next without a for.
Any advise would be appreciated.

Sub Mybaselinesub()
Dim i As Integer
For i = 2 To 5
Range("f3").FormulaR1C1 = "=DATE(YEAR(rc[1])-" & i & ",
MONTH(rc[1]), DAY(rc[1]))"

begbase = Range("f3").Value
Range("f3").Name = "begbase"
years = DateDiff("yyyy", begbase, reviewbeg)
Range("f4").Select
ActiveCell.FormulaR1C1 = "=match(r[-1]c,r[-2]c[-5]:r[" & numberrows
- 4 & "]c[-5],1)+2"
startbase = ActiveCell.Value
mybaseline = (endbase - startbase) + 1
If mybaseline = 10 Then
Trend 'next macro
'need to break the loop
'End Sub
End If

Next i

Trend 'next macro
End Sub

Tim Williams

For/Next with an If statement embedded
 

Try:

If mybaseline = 10 Then
Trend 'next macro
exit for
End If

Tim.

"Papa Jonah" wrote in message
...
I have a condition that throws me into this loop. However I need to
know how
to stop the loop per the If Then statement at the end.
Depending on how I have the ' marks, I either return to this
sub-routine or
I get an error that says I have a next without a for.
Any advise would be appreciated.

Sub Mybaselinesub()
Dim i As Integer
For i = 2 To 5
Range("f3").FormulaR1C1 = "=DATE(YEAR(rc[1])-" & i & ",
MONTH(rc[1]), DAY(rc[1]))"

begbase = Range("f3").Value
Range("f3").Name = "begbase"
years = DateDiff("yyyy", begbase, reviewbeg)
Range("f4").Select
ActiveCell.FormulaR1C1 = "=match(r[-1]c,r[-2]c[-5]:r[" &
numberrows
- 4 & "]c[-5],1)+2"
startbase = ActiveCell.Value
mybaseline = (endbase - startbase) + 1
If mybaseline = 10 Then
Trend 'next macro
'need to break the loop
'End Sub
End If

Next i

Trend 'next macro
End Sub





All times are GMT +1. The time now is 02:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com