I have an excel workbook set to manual calculation. I need a VBA code to calculate…
- User
I have an excel workbook set to manual calculation. I need a VBA code to calculate specific worksheets when I open them
- Assistant
Here is a VBA code that you can use:
Private Sub Workbook_Open() 'Enable the calculation for specific worksheets Worksheets("Sheet1").Calculate Worksheets("Sheet2").Calculate End Sub
Replace "Sheet1" and "Sheet2" with the names of the worksheets that you want to calculate.
You can also add more worksheets by duplicating the line "Worksheets("SheetName").Calculate" and changing the sheet name.
- User
I want to calculate only when I open the specific worksheet
- Assistant
Here's the updated code that calculates specific worksheets only when they are opened:
- Open VBA editor by pressing ALT + F11.
- Double-click the worksheet where you want to calculate upon opening.
- Paste the following code:
Private Sub Worksheet_Activate() ' Calculate the worksheet only when it's activated Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True End Sub
- Save the workbook.
Repeat steps 2 to 4 for every worksheet where you want to calculate upon opening.
Note: 'EnableEvents' is being turned off and on again to prevent the Worksheet_Activate event from triggering any other events like a macro or formula.
- User
I want this line of code to run only if the previous sheets were PREQUEST, PORODER, WORKST and GRN
- Assistant
Here's an updated code that will only calculate if the previous sheets were "PREQUEST", "PORODER", "WORKST", and "GRN":
Private Sub Worksheet_Activate() Dim prevSheet As Worksheet Dim sheetNames As Variant sheetNames = Array("PREQUEST", "PORODER", "WORKST", "GRN") Set prevSheet = Worksheets(Me.Index - 1) If IsError(Application.Match(prevSheet.Name, sheetNames, 0)) Then Exit Sub End If Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True End Sub
This code will first check if the name of the previous sheet is one of the four sheet names mentioned in the "sheetNames" array. If it's not, then it'll exit the sub and won't calculate. If it is, then it'll enable and disable events to prevent any other calculations from triggering, and it'll calculate the active sheet.
- User
ActiveSheet.Range(selectedCellAddress).Select - I want this line of code to run only if the previous sheets were PREQUEST, PORODER, WORKST and GRN
- Assistant
Here's the updated code that will select the specified range only if the previous sheets were "PREQUEST", "PORODER", "WORKST", and "GRN":
Private Sub Worksheet_Activate() Dim prevSheet As Worksheet Dim sheetNames As Variant sheetNames = Array("PREQUEST", "PORODER", "WORKST", "GRN") Set prevSheet = Worksheets(Me.Index - 1) If IsError(Application.Match(prevSheet.Name, sheetNames, 0)) Then Exit Sub End If Application.EnableEvents = False ActiveSheet.Range("A1").Select ' Change this to the address of your selected cell Application.EnableEvents = True End Sub
This code is similar to the previous one, but it selects range "A1" instead of the selected cell address because the active cell can't be selected if the sheet is not active. You can change "A1" to the address of your selected cell.
- User
The line of code is built into the Worksheet Activate event. I need an if rule for this. Here is the Worksheet Activate where the code ists Private Sub Worksheet_Activate() 'Select the same cell as in previous sheet ActiveSheet.Range(selectedCellAddress).Select Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True End Sub
- Assistant
Here's an updated code that selects the specified range only if the previous sheets were "PREQUEST", "PORODER", "WORKST", and "GRN":
Private Sub Worksheet_Activate() Dim prevSheet As Worksheet Dim sheetNames As Variant sheetNames = Array("PREQUEST", "PORODER", "WORKST", "GRN") Set prevSheet = Worksheets(Me.Index - 1) If Not IsError(Application.Match(prevSheet.Name, sheetNames, 0)) Then ' Select the same cell as in the previous sheet ActiveSheet.Range(selectedCellAddress).Select End If Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True End Sub
This code works as follows:
- It checks if the name of the previous sheet is one of the four sheet names mentioned in the "sheetNames" array. If it is, then it'll select the specified range.
- It then enables and disables events to prevent any other calculations from triggering, and then it calculates the active sheet.