+ Reply to Thread
Results 1 to 6 of 6

VBA code message box based on calculation on another sheet

  1. #1
    Registered User
    Join Date
    04-11-2013
    Location
    Bath, England
    MS-Off Ver
    Excel 2010
    Posts
    27

    VBA code message box based on calculation on another sheet

    Hi,

    I have the following code that prompts a message box if a calculated cell is more or less than 5% variance. This works perfectly on the sheet I have it set to, but I would like the calculation to take place on another worksheet ("Sheet 2"), but the message box to prompt on "Sheet 1".

    Also, I have several calculated cells on the same sheet, "H17", "H19" and "H20", can I repeat the same line of code below each one to check each calculation or is there an easier way?

    Thanks!

    Cd254


    Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("H17") > 0.5 Or Range("h17") < -0.5 Then
    MsgBox "WARNING - Calculation has exceeded limit"
    End If
    End Sub

  2. #2
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: VBA code message box based on calculation on another sheet

    Please Login or Register  to view this content.
    Apply to the sheet1 code to resolve the first part. I will look at the looping now

  3. #3
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: VBA code message box based on calculation on another sheet

    Done without looping:

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    04-11-2013
    Location
    Bath, England
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: VBA code message box based on calculation on another sheet

    That's great, thanks so much!

    I also have some more Subs on the same sheet, I thought I could integrate them into the same worksheet change event, but it keeps asking for an expected end sub...any ideas?!

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 31 Or Target.Column = 32 Then
    Call Bonus1
    End If
    Dim ws As Worksheet
    Set ws = Sheets("Sheet2")
    If ws.Range("H17") > 0.5 Or ws.Range("H17") < -0.5 Then
    MsgBox "WARNING - Calculation has exceeded limit"
    End If
    Sub Bonus1()
    Dim Key As Range
    For Each cel In Sheets("Sheet1").Range("AJ14:AJ1000" & Sheets("Sheet1").Cells(Rows.Count, "AJ").End(xlUp).Row)
    If cel.Value < 0 Then
    MsgBox "Error! Bonus value is negative!"
    Exit Sub
    End If
    Next cel
    Sub Bonus2()
    Dim Key As Range
    For Each cel In Sheets("Sheet1").Range("AE14:AF1000" & Sheets("Sheet1").Cells(Rows.Count, "AJ").End(xlUp).Row)
    If cel.Value < 0 Then
    MsgBox "Error! Value is negative!"
    Exit Sub
    End If
    Next cel
    Sub Bonus3()
    Dim Key As Range
    For Each cel In Sheets("Sheet1").Range("AE14:AF1000" & Sheets("Sheet1").Cells(Rows.Count, "AJ").End(xlUp).Row)
    If cel.Value < 0 Then
    MsgBox "Error! Value is negative!"
    Exit Sub
    End If
    Next cel
    End Sub

  5. #5
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: VBA code message box based on calculation on another sheet

    You have an If statement with no else statement but you have 3 outcomes although bonus 2 and bonus 3 are the same. I have made an interpretation but can you provide a bit more direction on what you want to achieve if this doesn't help.

    Maybe try:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    04-11-2013
    Location
    Bath, England
    MS-Off Ver
    Excel 2010
    Posts
    27

    Re: VBA code message box based on calculation on another sheet

    Thanks, I think Bonus 3 was an oversight on my part...

    Sub Bonus 1 is supposed to stop a negative value calculating in column AJ and Sub Bonus 2 is supposed to stop a negative value being inserted into columns AE or AF if that helps...

    Thanks again for your help.

+ 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. Pop up message based on date before opening spread sheet
    By rajasekar93 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-25-2015, 01:45 AM
  2. vba code to copy number of rows in based on the input message
    By prabhuduraraj09 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-12-2014, 12:38 AM
  3. Create an error message when you move to a different sheet based on a cell value
    By lhickerson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-17-2014, 06:11 PM
  4. [SOLVED] Worksheet change event code to display message box based on active cell offset content
    By Nitefox in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2014, 02:52 PM
  5. [SOLVED] Disabel sheet calculation based on checkbox
    By sblackman in forum Excel General
    Replies: 4
    Last Post: 07-17-2013, 09:09 PM
  6. Sheet calculation based on values within table
    By ccott23 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-27-2013, 07:29 AM
  7. Replies: 2
    Last Post: 08-09-2005, 12:05 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