how to combine two and more worksheet calculate event

Summer7sun

New Member
Joined
Sep 14, 2017
Messages
33
Code:
Private Sub Worksheet_Calculate()

Dim aRng As Range
Dim aCell As Range


Set aRng = Range("AE9:AE92")



For Each aCell In aRng
If aCell.Value = "Yes" Then
Call HitIt
Exit Sub
End If
Next
For Each aCell In aRng
If aCell.Value = "No" Then
Call HitIt
Exit Sub
End If
Next

End Sub


Code:
Private Sub Worksheet_Calculate()
Dim bRng As Range
Dim bCell As Range

Set bRng = Range("BH9:BH92")



For Each bCell In bRng
If bCell.Value = "Yes To Do" Then
Call HitIt2
Exit Sub
End If
Next
For Each bCell In bRng
If bCell.Value = "No Dont" Then
Call HitIt2
Exit Sub
End If
Next
End Sub



these code work only if one of them is in the worksheet calculate event ... The cell change is due to a formula .... and how to combine these code and in future how to combine a new one similar to this please help
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
See if this will work for you.
Code:
Private Sub Worksheet_Calculate()
Dim aRng As Range
Dim aCell As Range
Dim bRng As Range
Dim bCell As Range
Set aRng = Range("AE9:AE92")
    For Each aCell In aRng
        If aCell.Value = "Yes" Then
            Call HitIt
            GoTo PART2:
        End If
    Next
    For Each aCell In aRng
        If aCell.Value = "No" Then
            Call HitIt
            GoTo PART2:
        End If
    Next
PART2:
Set bRng = Range("BH9:BH92")
    For Each bCell In bRng
        If bCell.Value = "Yes To Do" Then
            Call HitIt2
            Exit Sub
        End If
    Next
    For Each bCell In bRng
        If bCell.Value = "No Dont" Then
            Call HitIt2
            Exit Sub
        End If
    Next
End Sub
 
Last edited:
Upvote 0
You can only have one event procedure in a sheet. Just combine the two - like below. This could be shortened but should work fine like this.
Code:
Private Sub Worksheet_Calculate()
Dim aRng As Range
Dim aCell As Range
Dim bRng As Range
Dim bCell As Range
Set aRng = Range("AE9:AE92")
For Each aCell In aRng
    If aCell.Value = "Yes" Then
    Call HitIt
    Exit For
    End If
Next aCell
For Each aCell In aRng
    If aCell.Value = "No" Then
    Call HitIt
    Exit For
    End If
Next aCell
Set bRng = Range("BH9:BH92")
For Each bCell In bRng
    If bCell.Value = "Yes To Do" Then
    Call HitIt2
    Exit For
    End If
Next bCell
For Each bCell In bRng
    If bCell.Value = "No Dont" Then
    Call HitIt2
    Exit For
    End If
Next bCell
End Sub
 
Upvote 0
@JoeMo, I could be wrong, but it looked to me like if the called macro ran for the Yes match, then the OP wanted to forego the second loop for that range. Using the 'Exit For' will allow the second loop to run for the same range.
JLG
 
Upvote 0
Thank you guys for your code, I got stuck with some extremely important work so could not update the result and it took a bit time. I would be able to test it in next 12 hrs and get back to you.... Thanks for your time JLGWhiz and JoeMo :)
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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