Results 1 to 7 of 7

Thread: If-Else-Range-Message Box Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2017
    Posts
    3

    If-Else-Range-Message Box Help

    Hi friends! I have just started learn visual basic and need your help if you can.

    I have a worksheet in which the user enter monthly sales to range E10:E46 row by row monthly. According to each sales number entered to this range, other cells in the same row have formulas and automatically calculate what I need to know basing on the sales number.

    I want excel to recognize each sales number when it is entered to the new cell in E10:E46. After this newly entered number, if the cumulative sum of the range from E10 to the lastly entered number (I mean E48) is bigger than the number in B3, it should warn me with a message box "Attention to red cells" sentence and let me make some other calculations after clicking OK button. If the number in E48 is equal or lower than the number in B3, it should give me a message box "No problem!" sentence and the message box should be closed after clicking the OK button.

    By myself, I wrote the code below for this purpose :

    Private Sub Worksheet_Change(ByVal Target As Range)
    Range("e48").Formula = "=Sum(e10:e46)"
    If Range("e48") > Range("b3") Then
    MsgBox "Attention to red cells!", vbOK
    Else
    MsgBox "No problem!", vbOK
    End If
    End Sub


    However, the problem is now the message boxes never closed. After clickin OK button or close button, it always renews the message box related to the if statement. I think I should add a code which can stop " if " after the clicking the OK button and works again after the new number entered into the following cell.

    Can you please help?

    Sincerely.
    Last edited by Kandiber; Jan 3rd, 2017 at 09:59 AM. Reason: mistake

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: If-Else-Range-Message Box Help

    Welcome to VBForums

    Thread moved to the 'Office Development/VBA' forum... note that while it certainly isn't made clear, the "VB Editor" in Office programs is actually VBA rather than VB, so the 'VB6' forum is not really apt

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: If-Else-Range-Message Box Help

    The following line of your code changes the worksheet:
    Code:
        Range("e48").Formula = "=Sum(e10:e46)"
    ...and changing the worksheet automatically calls the Sub again.

    To avoid that, either remove the line entirely (set the cell value manually, but you need to trust the users not to change it), or only set the value if it is different, eg:
    Code:
        If Range("e48").Formula <> "=Sum(e10:e46)" Then Range("e48").Formula = "=Sum(e10:e46)"

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2017
    Posts
    3

    Re: If-Else-Range-Message Box Help

    Dear si_the_geek, according to your response, I changed

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("e48") > Range("B3") Then
    MsgBox "Attention to red cells!"
    Else
    MsgBox "No problem"
    End If
    End Sub


    Now it works as I wanted and hope will not be problematic anymore.

    Thank you very much for your response.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: If-Else-Range-Message Box Help

    you can also set application.enableevents = false at the start of the sub, then any changes to cells within the sub will not fire the event again

    remember to set enableevents = true at the end of the procedure
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2017
    Posts
    3

    Re: If-Else-Range-Message Box Help

    Dear westconn1, thank you for response. Now the code works but it checks any change in any cell in the sheet and gives the message boxes as I want. However, how can fix it in order to get message boxes according to changes in the cells of E10:E46 range. Thanks...

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: If-Else-Range-Message Box Help

    try like
    Code:
    if not intersect(target, range("e10:e46")) is nothing then
    i just typed this in the browser, so may contain error
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width