Consulting

Results 1 to 15 of 15

Thread: Error alert if a certain limit is exceeded

  1. #1

    Error alert if a certain limit is exceeded

    Hi Folks,

    I attached a small Excel file to illustrate what I am trying to do. I have annual forecasts for four products to be broken down by quarter. The user will hard code the quarterly numbers the total of which must equal the annual forecast. On rows 9, 18, 27 and 36 I have provided "balance" cells so that the user can keep track of what amounts still need to be allocated.

    In cell B9 I show a hypothetical case where the quarterly totals exceed the annual budget. What I want to do is to have a warning message box appear if the number in any of the cells in rows 9, 18, 27, and 36 is less than zero, with the message "your quarterly amounts exceed the annual forecast," and an "OK" button which must be pressed before the user can continue.

    Can someone help me with that?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi K. Georgiadis,

    press Alt + F11 and double-click sheet1, then copy this code and paste in it:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Lin As Variant, i As Integer, Forec As Variant, Ano As String
    Lin = Array(9, 18, 27, 36)
    Forec = Array("B", "C", "D", "E", "F", "G")
    For Each linha In Lin
    For Each col In Forec
    If Range(col & linha) < 0 Then
    Ano = Range(col & linha - 6)
    z = MsgBox("Your quarterly amounts exceed the " & _
    "annual forecast for the year of " & Ano & ".", vbOKOnly)
    End If
    Next
    Next
    End Sub

    This will do what you want.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  3. #3
    Thank you. I'll give it a try

  4. #4
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Ok, and if anything goes wrong just post it here...
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  5. #5
    I tried it but there is a problem: after the warning message appears, I place the cursor on the offending cell to correct the number. As soon as I place the cursor on the cell, the warning message appears again before I get a chance to type a new number

  6. #6
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi,

    you may modify the Sub from
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    to
    Private Sub Worksheet_Change(ByVal Target As Range)
    ,

    this will make it to only check when you change a cell value.

    Is this better for you?
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  7. #7
    yes this works fine. Thanks! I'm marking it solved

  8. #8
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Great,

    if you need help again, just come back.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  9. #9
    Thanks again.

    Let the site administrator/approver take note: I have been visiting this site since June 2004. It is one of the finest, if not the finest, for getting prompt and expert advice. Kudos!

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Thank you very much! That is very kind of you.

    We all do what we can, and take pride in our work. We are quite fortunate to have some of the worlds finest coders! A special thanks to Paleo for such an outstanding job and prevelant commitment!!

    Quote Originally Posted by K. Georgiadis
    Thanks again.

    Let the site administrator/approver take note: I have been visiting this site since June 2004. It is one of the finest, if not the finest, for getting prompt and expert advice. Kudos!

  11. #11
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Thank you very much Zack .

    But hey remember I have the best master that are out there.

    I have learned a lot from you, Jake and John.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  12. #12
    What have I started?
    But, really guys, when I come to this site with a question, I know that I'm going to find an answer. Don't be surprised if you see me again soon...

  13. #13
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Get back whenever you want we will all be wanting to help you out.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  14. #14
    Don't you worry. I'm posting a new request separately right now!

  15. #15
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Great and I am going there to check it out... Hope I may help you out.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

Posting Permissions

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