+ Reply to Thread
Results 1 to 7 of 7

How to show MsgBox if a any cell value is > 17 in a range

Hybrid View

  1. #1
    Registered User
    Join Date
    12-09-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    13

    How to show MsgBox if a any cell value is > 17 in a range

    Hi all

    I've been working on 2 pieces of coding all weekend but seem to not get round it at all.

    I'm trying to to show a message to the user when a value is >17 in any cell of a range (c7:g13).


    I have the standard code:

    if range ("c7") >17 then
    MsgBox "one of the cells value is greater then 17" 
    End if

    i want it to look through my whole range (c7:g13) and if any cell has a value greater then 17 then show the message.

    Can you help?



    Also my second code i was looking for is similar, I'm doing a worksheet change event when columns 7 and 8 value = date and time on another sheet, to show an input box.


    Please help, my hairs literally gone white thinking about this all weekend. I think i mjust being stupid and missing something obvious.

    Thanks in advance.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: How to show MsgBox if a any cell value is > 17 in a range

    If WorksheetFunction.CountIf(Range("C3:G13"), ">17") Then
        MsgBox "One of the cell values is greater than 17"
    End If
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Registered User
    Join Date
    12-09-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    13

    Unhappy Re: How to show MsgBox if a any cell value is > 17 in a range

    Hi Palmetto

    Thanks for replying so quick but the code doesn't do anything, no errors etc and no messages come up when one of the cells in the range is greater then 17.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: How to show MsgBox if a any cell value is > 17 in a range

    Try this
    Sub Button1_Click()
        Dim rng As Range, c As Range
        Set rng = Range("C7:G13")
        For Each c In rng.Cells
            If c.Value > 17 Then MsgBox c.Address & " is greater then 17"
        Next c
    End Sub

  5. #5
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: How to show MsgBox if a any cell value is > 17 in a range

    The code I gave you works just fine for me. How id you attempt to use it?

    However, if you want notification for each individual cell in the range then davesexcel code will do just that. Just be aware that multiple message boxes will be displayed and have to be dealt with - could get annoying.

    Also my second code i was looking for is similar, I'm doing a worksheet change event when columns 7 and 8 value = date and time on another sheet, to show an input box.
    I suggest you post a sample workbook, or at least upload the code.

  6. #6
    Registered User
    Join Date
    12-09-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: How to show MsgBox if a any cell value is > 17 in a range

    Thanks Palmetto davesexcel code worked. but one other problem i am having with this is the range actually isn't c7:g13 its actual c7:g13 and then again from k7:o13 and then again from y7:ac13.

    I've tried to insert the code as this

    Sub Calculate_Worksheet()
        Dim rng As Range, c As Range
        Set rng = Range("c7:g13, k7:o13, y7:ac13")
         
        For Each c In rng.Cells
            If c.Value > 16 Then MsgBox " Too many students have been booked in please check the following Cell " & c.Address
        Next c
        
    End sub

    but this gives me a global error. can you help? I've added an attachment should you need to have a look at a sample.
    Attached Files Attached Files
    Last edited by syedalamgir; 01-12-2011 at 07:38 PM. Reason: typing error

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: How to show MsgBox if a any cell value is > 17 in a range

    If you are wanting the code to activate when the sheet calculates, then this needs to be placed in the worksheet module
    Private Sub Worksheet_Calculate()
    
        Dim c As Range, rng As Range
    
        Set rng = Range("C7:G13,K7:O13,Y7:AC13,AG7:AK13")
    
        For Each c In rng.Cells
    
            If c > 16 Then MsgBox " Too many students have been booked in please check the following Cell " & c.Address
    
        Next c
    
    End Sub
    It will seem to take a long time for this to kick in as you have your formulas calculating 65536 rows.

    Where the code goes

+ 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