Multiple worksheet change events in one sheet

vekdas

New Member
Joined
Jan 26, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi

Can anyone help - I am trying to create two worksheet changes which need to happen as a result of a change in a cell. There are two sets of code below which I believe I need to try and combine but I am not sure how??

The code for the first change is: (this works fine on its own - it hides rows wherever the word 'delete' is in a specific cell within that row (within a range called "Trusts2" , otherwise it does not hide the row.
This code has to run through a number of rows to decide whether to hide or show the row.


Private Sub Worksheet_Change(ByVal Target As Range)

Dim xRg As Range

Application.ScreenUpdating = False

For Each xRg In Range("Trusts2")
If xRg.Value = "Delete" Then
xRg.EntireRow.Hidden = True

Else

xRg.EntireRow.Hidden = False

End If

Next xRg

Application.ScreenUpdating = True

End Sub



The second code I want to add to this should update all cells in a known range to "Yes" on the same sheet. If I was just writing a simple macro for it, it would look like this:

Sub Reset_Comparison_Trusts()

Sheet15.Range("YesorNo2").Value = "Yes"

End Sub


Many thanks for any help in showing how best to combine these!
 
Hi Kev,

from looking at your workbook I think it will be good enough to just monitor changes to C3 to trigger the code: change the contents in Range YesorNo2 to Yes, make all rows for the range Trusts2 and loop through Range Trusts2 to hide all rows where Delete is found. I still try to find out what you mean by



The code so far as described above:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rngCell As Range

'allow only one cell to be changed to start the processing
If Target.Count > 1 Then Exit Sub

Application.ScreenUpdating = False

If Not Intersect(Target, Range("C3")) Is Nothing Then
  Application.EnableEvents = False
  Range("YesorNo2").Value = "Yes"
  Range("Trusts2").EntireRow.Hidden = False
  Application.EnableEvents = True
  For Each rngCell In Range("Trusts2")
    rngCell.EntireRow.Hidden = (rngCell.Value = "Delete")
  Next rngCell
End If

Application.ScreenUpdating = True

End Sub

As you can see from my profile I can run the code but will get just errors as I'm still using 2019. So I kept the data by using a different cell to trigger.

Ciao,
Holger
Hi Holger

This code works perfectly - thank you so much for taking the time to help!

Best Wishes

Kev
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi Kev,

thanks for the feedback, glad to be of help.

Holger
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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