+ Reply to Thread
Results 1 to 4 of 4

VBA IF Statement to return MsgBox if True and Stop Macro

  1. #1
    Registered User
    Join Date
    07-05-2012
    Location
    Ontario, California
    MS-Off Ver
    Excel 2007
    Posts
    2

    VBA IF Statement to return MsgBox if True and Stop Macro

    Hello,

    I am trying to wrirte a simple IF statement in VBA code, but it has become more challenging than expected. I have a column (column D) within a work sheet called "custom cost sheet". Column D lists whether a cost is correct or not. I want to create a VBA code that will scroll through Column D in search for the word "incorrect cost", if the word exists I want a Msgbox to appear stating "cost error, fix cost and recalculate" and then stop the macro from continuing. I just want the Msgbox to appear for the first cell it finds containing "incorrect cost", not each cell. If the text is not found within the column, I want the macro to continue and finish running.

    Any help with this would be extremely appreciated.

    Thanks in advance!

    Rene
    Last edited by Forge463; 07-05-2012 at 08:00 PM. Reason: Changed Title

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA IF Statement to return MsgBox if True and Stop Macro

    Hello and welcome to the forum.

    First of all avoid loops like the plague wherever you can and use standard Excel functionality like formulas or filtering etc. Loops are generally less efficient.

    In this case just add the following formula in a spare cell and name the cell say 'check'
    =IFERROR(MATCH("incorrect cost",D:D,False),"error")

    Now the macro will be

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    05-25-2012
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    194

    Re: VBA IF Statement to return MsgBox if True and Stop Macro

    Please Login or Register  to view this content.
    **edit** - Richard is right though - the above is more efficient, and the code I provided could error out (such as if the range D1:D"XYZ" only had 1 entry, etc - I wrote it basically assuming you would have more than 2 entries in column D, began at cell D1, and had no spaces between data (e.g. empty rows in column D))
    Last edited by Medpack; 07-05-2012 at 08:12 PM.

  4. #4
    Registered User
    Join Date
    07-05-2012
    Location
    Ontario, California
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: VBA IF Statement to return MsgBox if True and Stop Macro

    Thanks all! I found Richard's work around to work well!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1