Consulting

Results 1 to 5 of 5

Thread: Solved: End For Next routine on error

  1. #1
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location

    Solved: End For Next routine on error

    Morning
    How do end this routine if there is no price?

    (without holding down the enter key for ages doh! )

    [vba]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[/vba]

  2. #2
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location

    Sussed it (I think)

    Well this seems to work
    [vba]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:[/vba]

    Is that the correct way?

  3. #3
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by lifeson
    ...Is that the correct way?
    No, try Exit For
    [vba]
    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
    [/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Instead of "Goto GetPrice", use "Exit For", which escapes the loop
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Quote Originally Posted by johnske
    No, try Exit For
    [vba]
    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
    [/vba]
    I knew it was too good to be true

    Thanks for pointing me in the right direction though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •