+ Reply to Thread
Results 1 to 6 of 6

Testing a condition and generating a message box

Hybrid View

  1. #1
    Registered User
    Join Date
    12-11-2007
    Posts
    15

    Testing a condition and generating a message box

    Hello all,

    I'm trying to create a troubleshooting sheet for something that I need to do at work. Basically, I'm trying to catch a problem. I have an orderbook for a particular stock with the best bid and offers -- they are supposed to be in multiples of 100,000, however sometimes we are seeing that this is not the case. I am using the modulus operator to test any time that the values are not divisible by 100,000, so in a perfect world I am always getting a value of 0.

    Id like to create a macro that looks at my range of 10 modulus cells and tests to see if they are equal to 0. If they aren't, I'd like to pop up a message box to alert me. Also I'd like the test to run continually so I suppose it would have to be set off by a worksheet change event. I attached a very basic example sheet but need to know if what im trying to do is possible in VB.

    The modulus range I want to test is C9:D13 on sheet 2
    Attached Files Attached Files

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Testing a condition and generating a message box

    Hi,

    Rather than using a macro, could you instead use data validation to ensure that your error condition is never met?

    Use Custom Data Validation, =MOD($C3,100000)=0 where C3 is the top left corner of the selected cells.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    12-11-2007
    Posts
    15

    Re: Testing a condition and generating a message box

    I tried that, however I am not directly inputting values to where the modulus test is going on, it is updating based on whatever happened in C3 or whichever cell it is looking to. Therefore it was not generating the error (unless I manually tried to input a non 0 value in one of the ranges).

  4. #4
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Testing a condition and generating a message box

    You could use this code as a start point.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rTestRange As Range, rCell As Range
    
    Set rTestRange = Range("C9:D13")
    For Each rCell In rTestRange
    If rCell.Value <> 0 Then MsgBox (rCell.Address & " is not zero")
    Next
    
    
    End Sub
    To enter this code, right click the worksheet tab and select view code. Paste the code into the VB editor that pops up, then close the window.

  5. #5
    Registered User
    Join Date
    12-11-2007
    Posts
    15

    Re: Testing a condition and generating a message box

    Sweep, I was able to use that code and make it work with my sheet. Thanks!

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Testing a condition and generating a message box

    Why not using conditionalformat ?

    =MOD(C3,10^5)<>0
    Last edited by snb; 06-28-2010 at 05:39 AM.

+ 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