View Full Version : [SOLVED:] End For Next routine on error
lifeson
09-18-2007, 12:58 AM
Morning :hi: 
How do end this routine if there is no price?
 
(without holding down the enter key for ages  doh! :whistle: :doh: ) 
 
For i = 2 To iLastRow
    If Cells(i, "A").Value = sPrem And Cells(i, "D").Value = "CHF" Then
        dPrice = Cells(i, "C").Value
        MsgBox sPrem & dPrice
    Else
        MsgBox "No price" 'how do i stop routine here?
    End If
Next i
lifeson
09-18-2007, 01:12 AM
Well this seems to work
For i = 2 To iLastRow
    If Cells(i, "A").Value = sPrem And Cells(i, "D").Value = sProd Then
        dPrice = Cells(i, "C").Value
        MsgBox sPrem & dPrice
    Else
        MsgBox "No price"
        GoTo GetPrice
    End If
Next i
GetPrice:
 
Is that the correct way?
johnske
09-18-2007, 01:18 AM
...Is that the correct way?No, try Exit For
 
For i = 2 To iLastRow
    If Cells(i, "A").Value = sPrem And Cells(i, "D").Value = "CHF" Then
        dPrice = Cells(i, "C").Value
        MsgBox sPrem & dPrice
    Else
        MsgBox "No price" 'how do i stop routine here?
        Exit For
    End If
Next i
mdmackillop
09-18-2007, 01:19 AM
Instead of "Goto GetPrice", use "Exit For", which escapes the loop
lifeson
09-18-2007, 01:35 AM
No, try Exit For
 
For i = 2 To iLastRow
    If Cells(i, "A").Value = sPrem And Cells(i, "D").Value = "CHF" Then
        dPrice = Cells(i, "C").Value
        MsgBox sPrem & dPrice
    Else
        MsgBox "No price" 'how do i stop routine here?
        Exit For
    End If
Next i
 
I knew it was too good to be true
:doh: 
Thanks for pointing me in the right direction though.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.