Can I have a VBA code that does the following: If any change is made to the ranges,…
- User
Can I have a VBA code that does the following: If any change is made to the ranges, D2:D22, F2:F22, H2:H22, J2:J22, N2:N22, P2:P22, R2:R22, T2:T22, V2:V22, X2:X22, then B2:B23 and Z2:Z23 should be calculated.
- Assistant
Sure! Here's a VBA code that will calculate the values in cells B2:B23 and Z2:Z23 whenever any change is made in the specified ranges:
vbaPrivate Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range Set rng = Intersect(Target, Range("D2:D22, F2:F22, H2:H22, J2:J22, N2:N22, P2:P22, R2:R22, T2:T22, V2:V22, X2:X22")) If Not rng Is Nothing Then Application.EnableEvents = False Range("B2:B23, Z2:Z23").Calculate Application.EnableEvents = True End If End SubYou can add this code to the Worksheet module where you want this functionality to be implemented. To do so, follow these steps:
- Press
ALT + F11to open the Visual Basic editor. - In the Project Explorer window, find the Worksheet name where you want to implement the code.
- Double-click on the Worksheet name to open the code window.
- Paste the provided code into the code window.
- Press
- User
Is there a VBA code that will Password Unprotect and set Enable Events to Flase for only the Active sheet?
- Assistant
Yes, here's a VBA code you can use to password unprotect the active sheet and set
EnableEventstoFalsespecifically for the active sheet:vbaSub PasswordUnprotectActiveSheet() Dim ws As Worksheet Set ws = ActiveSheet ws.Unprotect Password:="YourPassword" Application.EnableEvents = False ' Your code here Application.EnableEvents = True ws.Protect Password:="YourPassword" End SubReplace "YourPassword" with the actual password of your worksheet. The code will unprotect the active sheet, set
EnableEventstoFalse, and perform the necessary operations. Finally, it will setEnableEventsback toTrueand protect the active sheet with the password.You can run this code by pressing
ALT + F11to open the Visual Basic Editor, then go toInsert>Moduleto insert a new module. Paste the code into the module. Now you can run the code by pressing or manually running the subroutine from the Macro dialog box (press to access it). - User
what is the VBA event to use when a sheet Deactivates
- Assistant
The VBA event to use when a sheet deactivates is the
Worksheet_Deactivateevent. This event occurs when a different sheet is activated and the current sheet becomes inactive. You can place your code inside this event to perform any actions you want when the sheet deactivates.Here's an example of how to use the
Worksheet_Deactivateevent:vbaPrivate Sub Worksheet_Deactivate() ' Your code here MsgBox "Sheet has been deactivated." End SubIn this example, a
MsgBoxis displayed when the sheet is deactivated. You can replace this line with your own code to perform any desired actions when the sheet is deactivated. To implement this event, follow these steps:- Press
ALT + F11to open the Visual Basic editor. - In the Project Explorer window, find the worksheet where you want to implement the code.
- Double-click on the worksheet name to open the code window.
- Press
- User
I am getting an error on this code: Private Sub Worksheet_Deactivate() Application.EnableEvents = True ThisWorkbook.Worksheets("Stector Budget").Protect Password:="edit" End Sub
- Assistant
The error you're encountering is likely due to the fact that the
Protectmethod is being executed without specifying the respective sheet. To resolve this, you can modify the code to include the specific sheet where you want the protection to be applied. Here's an updated version of the code:vbaPrivate Sub Worksheet_Deactivate() Application.EnableEvents = True ThisWorkbook.Worksheets("Sheet Name Here").Protect Password:="edit" End SubEnsure that you replace "Sheet Name Here" with the actual name of the sheet where you want the protection to be enabled. Additionally, make sure that the sheet name is enclosed in double quotation marks.
Please note that this code should be added to the worksheet module of the sheet where you want the
Worksheet_Deactivateevent to occur. To do this, follow the steps below:- Press
ALT + F11to open the Visual Basic Editor. - In the Project Explorer window, locate and double-click on the sheet where you want to implement the code.
- Press