+ Reply to Thread
Results 1 to 9 of 9

Excel VBA event handling

  1. #1
    Registered User
    Join Date
    04-04-2013
    Location
    Maplewood, MN
    MS-Off Ver
    Excel 2010
    Posts
    14

    Excel VBA event handling

    This is my code on vba for a excel sheet:
    Please Login or Register  to view this content.
    It works but only when I manually click on a cell and change a value to negative. What I want is whenever any value on any sheet gets decremented to a negative number it pops up a msg warning me that a cell value is < 0. For example, if I request for 10 items in my inventory and I only have 9 quantity, the quantity which =sum(quantity - request) becomes negative and a popup msg will appear. The code up there doesn't really work, anyone know a way around this?
    Last edited by Leith Ross; 04-04-2013 at 11:13 PM. Reason: Added Code Tags

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Excel VBA event handling

    Removing the quotes around 0 might help.

    If you wanted this code to work on all sheets you would need to use the SheetChange event of ThisWorkbook.
    Please Login or Register  to view this content.
    If the values will be changing as a result of a formula you probably want to look at SheetCalculate.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Excel VBA event handling

    This type of code will help you deal with negatives as a result of formulas. As it is setup, it looks at any of the cells in Column B (Column (2) in the code. Modify to suit.

    I will warn that this code fires whenever the sheet calculates, which happens with nearly every change. That said, it can cause your file to run more slowly as the file size increases. Let me know if it helps.

    Please Login or Register  to view this content.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Excel VBA event handling

    Hello vangxbg,

    Welcome to the Forum!

    What you want to do can probably be done better. You should post your workbook for review then we can evaluate what you are doing and offer you better suggestions and solutions.

    To Post Your Workbook
    1. At the bottom right of the Reply window, Click the button Go Advanced
    2. At the top of the Your Message Window, in the first row of icons, Click the Paperclip icon.
    3. Click the Add Files button at the top right in the dialog box. This displays the File Manager dialog.
    4. Click the Select Files button at the bottom middle of the dialog.
    5. In the new window Find the file you want to upload, select it, and Click Open.
    6. You will now be back in the File Manager dialog. Click the bottom Middle button Upload File.
    7. Wait until the file has completely uploaded before you exit the File Manager dialog.

    File Manger Picture
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    04-04-2013
    Location
    Maplewood, MN
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Excel VBA event handling

    Thanks Norie, workbook_sheetchange is a step closer to what I want. So you mentioned using sheet_calculate, do I have to user it on every sheet then?

  6. #6
    Registered User
    Join Date
    04-04-2013
    Location
    Maplewood, MN
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Excel VBA event handling

    Thanks bigbas that's kind of exactly what I wanted. Do I put that code on every worksheet? When I put it on worksheet10 and change values in worksheet1 making values in worksheet10 go negative, it throws me some kind of method intersect of global failed. It does work if I'm actually on worksheet10 though. Also it's giving me a msgbox everytime an yvalue changes whether is negative or positive, but I think I can probably figure how to fix the code so it only does it for negative values only.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Excel VBA event handling

    I mentioned SheetCalculate, which is a workbook level event similar to SheetChange.

    The main difference is that SheetCalculate is triggered whenever a sheet in the workbook is calculated.

  8. #8
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Excel VBA event handling

    Based on my understanding (and anyone else can chime in if I'm incorrect), the Workbook Scope SheetCalculate code may not be appropriate if there are linkages among sheets. I believe that Excel runs the code for each sheet subsequently; if multiple sheets link to an original sheet, the code will run for each dependent sheet. For example, if Sheets 2, 3 and 4 each have a formula that links to sheet 1, then if this value becomes negative, you will get 3 separate message boxes (1 per sheet).

    I believe the BEST approach is to write code in a module, and have that code called whenever there is a workbook change. This is a two step process. First, enter the below code in a STANDARD MODULE. Note: as per before, the code will check column 2 for negative values, as defined by the Const col As Long = 2
    line of code

    Please Login or Register  to view this content.
    Next, enter the following code in the ThisWorkbook module:

    Please Login or Register  to view this content.
    Report back if this helps.

  9. #9
    Registered User
    Join Date
    04-04-2013
    Location
    Maplewood, MN
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Excel VBA event handling

    Thanks it works now!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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