How to Insert Workbook_SheetCalculate Event

HJA14

Board Regular
Joined
Apr 12, 2016
Messages
60
Dear all,

I have written the following code:

Code:
Workbook_SheetCalculate(ByVal Sh As Worksheet)
    Dim myRange          As Range
    Dim product          As Double
    Dim n                As Long


    Set myRange = Sh.Range("N1:N50,AA1:AA50,AN1:AN50,BA1:BA50,BN1:BN50,CA1:CA50")
    Productroduct = Sh.Range("A1").Value * Sh.Range("A2").Value
    For n = 1 To myRange.Count
        If myRange(n) > Product Then
            MsgBox "Ticker: " & Sh.Name & ". Today's abnormal volume in the  " & myRange(n).Offset(, -3).Value & " serie is " & myRange(n) & " contracts."
			myRange(n).ColorIndex = 6
			Sh.Select
        End If
    Next n
End Sub

However, it does not run. If try to insert a module and press F5, the Macro box appears. And I am unable to press Run. Maybe I am missing something, and I would love to know what exactly!!

Thank you
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
First:
Code:
[COLOR=#ff0000][B]Private Sub [/B][/COLOR]Workbook_SheetCalculate(ByVal Sh As [COLOR=#ff0000][B]Object[/B][/COLOR])

Second, this procedure must be located in ThisWorkbook module.
 
Upvote 0
That is because this code runs when an event occurs, , macros based on events don't show up when you press F5 because they should be triggered by an event in the application itself.

In this case the code runs each time a calculation changes on a certain worksheet.

For further explanation:
Events In Excel VBA

Best regards,
 
Upvote 0
Thank you for the reply. I have included the module in "ThisWorkbook".


Code:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
   With Sh
    Dim myRange          As Range
    Dim product          As Double
    Dim n                As Long
    Set myRange = Sh.Range("N1:N50,AA1:AA50,AN1:AN50,BA1:BA50,BN1:BN50,CA1:CA50")
    Set product = 0.001 * Sh.Range("A3").Value * Sh.Range("A5").Value
    For n = 1 To myRange.Count
        If myRange(n) > product Then
            MsgBox "Ticker: " & Sh.Name & ". Today's abnormal volume in the  " & myRange(n).Offset(, -3).Value & " serie is " & myRange(n) & " contracts."
            myRange(n).ColorIndex = 9
            Sh.Select
        End If
    Next n
End Sub

Is it possible that you take a look at my code and give feedback?
 
Upvote 0
Code:
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    
    Dim myRange As Range
    Dim product As Double
    Dim n As Long
    Dim rng As Range, rngArea As Range
    
    With Sh
        Set myRange = .Range("N1:N50,AA1:AA50,AN1:AN50,BA1:BA50,BN1:BN50,CA1:CA50")
        Set product = 0.001 * .Range("A3").Value * .Range("A5").Value
        For Each rngArea In myRange.Areas
            For Each cell In rngArea
                If cell > product Then
                    MsgBox "Ticker: " & .Name & ". Today's abnormal volume in the  " & cell.Offset(, -3).Value & " serie is " & cell & " contracts."
                    cell.ColorIndex = 9
                    'Sh.Select - why select in a loop?
                End If
            Next
        Next
    End With
    
End Sub

End Sub
 
Upvote 0
You're welcome!
hi.gif
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,219
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top