+ Reply to Thread
Results 1 to 12 of 12

easy .... Combine 2 Worksheet_Change Events

Hybrid View

  1. #1
    Registered User
    Join Date
    01-09-2014
    Location
    Durham, NC
    MS-Off Ver
    Excel 2003
    Posts
    8

    easy .... Combine 2 Worksheet_Change Events

    I'm learning VBA and I have two worksheet change events that I need to combine. The first calls a userform when the user selects "other" from a drop-down

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Set Target = Range("C9")
    If Target.Value = "Other" Then
    SmallUtilityData.Show
    End If
    
    End Sub
    With the second I am trying to monitor cell E84....everytime the cell's value changes (e.g. is recalculated), I want to compare it to cell C11. If the value in C11 is less than the calculated value in C84, I would like to display a message box.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Set Target = Range("E84)
    If Target.Value > Range("C11").value Then
    MsgBox "The array area exceeds the available roof space", vbCritical, "Warning!"
    
    End If
    
    End Sub

    Please help, explanations appreciated.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: easy .... Combine 2 Worksheet_Change Events

    This is how I would do that:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    For Each cell In Target
    
        Select Case cell.Address
            Case Is = "$E$84"
                If cell.Value > Range("C11").Value Then MsgBox "The array area exceeds the available roof space", vbCritical, "Warning!"
            
            Case Is = "$C$9"
                If cell.Value = "Other" Then SmallUtilityData.Show
        
            End Select
    Next cell
    End Sub
    Last edited by JBeaucaire; 05-18-2015 at 09:31 PM. Reason: Corrected a coding typo.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    01-09-2014
    Location
    Durham, NC
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: easy .... Combine 2 Worksheet_Change Events

    The cell E84 code is not getting called here, only the second one.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: easy .... Combine 2 Worksheet_Change Events

    Small typo fixed above.

  5. #5
    Registered User
    Join Date
    01-09-2014
    Location
    Durham, NC
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: easy .... Combine 2 Worksheet_Change Events

    It still does not fire for the E84 event

  6. #6
    Registered User
    Join Date
    01-09-2014
    Location
    Durham, NC
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: easy .... Combine 2 Worksheet_Change Events

    Could calling the userform be ending the sub?

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: easy .... Combine 2 Worksheet_Change Events

    Only one cell at a time is being checked so it should trigger the first section of code if cell E84 is the one that is changed. The code looks correct, I can't advise more without being able to test it on your workbook.

  8. #8
    Registered User
    Join Date
    01-09-2014
    Location
    Durham, NC
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: easy .... Combine 2 Worksheet_Change Events

    Okay. I just realized The message box is only triggered if you manually change the value of cell E84. Cell E84 contains a formula though and I can't change that. I need to monitor the result of the formula.

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

    Re: easy .... Combine 2 Worksheet_Change Events

    What's the formula in E84?

    Does it contain references to any cells that are being changed manually?
    If posting code please use code tags, see here.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: easy .... Combine 2 Worksheet_Change Events

    The EVENT you may want to switch to then, is Worksheet_Calculate() event. The only problem with that is the things you want to "pop up" on the two watched cells could BOTH happen every time, it might get weird.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    For Each cell In Target
        Select Case cell.Address
            Case Is = "$C$9"
                If cell.Value = "Other" Then SmallUtilityData.Show
            End Select
    Next cell
    End Sub
    
    Private Sub Worksheet_Calculate()
    
    If Range("$E$84").Value > Range("C11").Value Then
        MsgBox "The array area exceeds the available roof space", vbCritical, "Warning!"
    End If
    
    End Sub

  11. #11
    Registered User
    Join Date
    01-09-2014
    Location
    Durham, NC
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: easy .... Combine 2 Worksheet_Change Events

    I switched the cell being watched to the manual input cell that the E84 formula is linked too. Works great thank you.!

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: easy .... Combine 2 Worksheet_Change Events

    Very smart! If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Combining two Worksheet_Change Events
    By Fett2oo5 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-09-2013, 01:05 PM
  2. [SOLVED] Merging Two Worksheet_Change Events
    By swordswinger710 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-04-2012, 04:18 PM
  3. Multiple worksheet_change events on one sheet
    By Hammer_757 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-02-2012, 03:10 PM
  4. WorkSheet_Change or Events
    By fsgg69 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-28-2010, 04:09 AM
  5. whats the maximum nr of Worksheet_Change events?
    By diepenbos in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-21-2006, 03:54 PM

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