Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Sam Sam is offline
external usenet poster
 
Posts: 4
Default Continuing a loop from within an if statement?

I need to go to the next iteration of my loop from within my IF
statement. How is this done? I can't belive I can't find such a simple
thing on my own...
For i = 1 To lastrow
...do something
If Mid(str1, 1, 3) = "USA" Then
... do something else
Next ///// gives me compile error "next without for"
End If
...do something
Next

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 244
Default Continuing a loop from within an if statement?

Not sure what logic you are trying to test here - you cannot have more
than one next per for statement, but you could have

for i = 1 to last throw
if mid(str1,1,3)="USA" then
'do something else
else
'do something
end if
next

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Continuing a loop from within an if statement?

I haven't tested this, but maybe try:

For i = 1 To lastrow
...do something
If Mid(str1, 1, 3) = "USA" Then
... do something else
Goto ForceNexti
End If
...do something
ForceNexti:
Next

  #4   Report Post  
Posted to microsoft.public.excel.programming
Sam Sam is offline
external usenet poster
 
Posts: 4
Default Continuing a loop from within an if statement?

Thank you. I was looking for a simple "continue" type statement. I
fixed it by adding your loop above.
Thjnak you very much.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Continuing a loop from within an if statement?

Hi Sam,

Try something like:

'=============
Public Sub Tester001()
Dim sStr As String
Dim i As Long
Dim LastRow As Long

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To LastRow
sStr = ActiveSheet.Cells(i, "A").Value
If Mid(sStr, 1, 3) = "USA" Then
'Do something, e.g.:
Cells(i, "A").Interior.ColorIndex = 3
Else
'Do something else
End If
Next i

End Sub
'<<=============


---
Regards,
Norman


"Sam" wrote in message
oups.com...
I need to go to the next iteration of my loop from within my IF
statement. How is this done? I can't belive I can't find such a simple
thing on my own...
For i = 1 To lastrow
...do something
If Mid(str1, 1, 3) = "USA" Then
... do something else
Next ///// gives me compile error "next without for"
End If
..do something
Next





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Continuing a loop from within an if statement?

Aiden gave the best solution, but if you can't figure out a way to construct
your if statements to do it in a structured fashion, you might be able to do
something like

For i = 1 To lastrow
bDoMore = True
...do something
If Mid(str1, 1, 3) = "USA" Then
... do something else
bDoMore = False
End If
if bDoMore then
....do something
end if
Next

--
Regards,
Tom Ogilvy


"Sam" wrote:

I need to go to the next iteration of my loop from within my IF
statement. How is this done? I can't belive I can't find such a simple
thing on my own...
For i = 1 To lastrow
...do something
If Mid(str1, 1, 3) = "USA" Then
... do something else
Next ///// gives me compile error "next without for"
End If
...do something
Next


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 244
Default Continuing a loop from within an if statement?

You could always have your DoSomething and DoSomething else bits as sub
routines - which is always quite nice in code anyway

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Continuing a loop from within an if statement?

try

If InStr(Cells(ActiveCell.Row, ActiveCell.Column), "USA") Then MsgBox "Hi"
or for str1

If InStr(str1, "USA") Then dosomething

NO neeed for
else
do something else
end if
IF on same line or you used the continuation character _

--
Don Guillett
SalesAid Software

"Sam" wrote in message
oups.com...
I need to go to the next iteration of my loop from within my IF
statement. How is this done? I can't belive I can't find such a simple
thing on my own...
For i = 1 To lastrow
...do something
If Mid(str1, 1, 3) = "USA" Then
... do something else
Next ///// gives me compile error "next without for"
End If
..do something
Next



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
for loop with if statement Steve Excel Worksheet Functions 1 February 17th 10 07:56 PM
loop Statement Nigel Excel Programming 0 April 4th 06 04:10 PM
If statement - Loop? George Excel Discussion (Misc queries) 1 March 14th 06 07:06 AM
Help with a loop VBA with an if statement kixelsid Excel Programming 10 February 28th 06 07:15 PM
Do Until loop with if statement Sandy[_3_] Excel Programming 4 July 17th 03 11:06 AM


All times are GMT +1. The time now is 01:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"