When my excel work book opens, I want all the sheets in the workbook to be set to manual…
- User
When my excel work book opens, I want all the sheets in the workbook to be set to manual except Sheet 1, Sheet 2 and Sheet 3. These 3 sheets must continue to calculate automatically while other sheet in the workbook calculate manually. How can I do this.
- Assistant
You can use VBA code to achieve this. Here's the code:
- Open the workbook and press Alt + F11 to access the Visual Basic Editor.
- In the Project Explorer window, double-click the ThisWorkbook module.
- In the window that appears, paste the following code:
Private Sub Workbook_Open() Dim ws As Worksheet For Each ws In Worksheets If ws.Name <> "Sheet1" And ws.Name <> "Sheet2" And ws.Name <> "Sheet3" Then ws.EnableCalculation = False End If Next ws End Sub- Save the workbook and close the Visual Basic Editor.
- When you reopen the workbook, all sheets except for Sheet1, Sheet2, and Sheet3 will be set to manual calculation.
Note: This code only sets the calculation mode for the worksheets when the workbook is opened. If you add new worksheets to the workbook, you will need to run the code again to set their calculation mode.
- User
In the following code, if a cell is not selected in the array of sheets, I get an error. How can I write the code so that it is skipped if no cell is selected in any sheet of the array. Private Sub Workbook_SheetActivate(ByVal Sh As Object) Dim prevSheet As Worksheet Dim sheetNames As Variant sheetNames = Array("PORDER", "WORKST", "WORKPR", "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
- Assistant
You can use an If statement to check if there is a selected cell in any sheet of the array. If there is no selected cell, then you can exit the sub using the Exit Sub statement. Here's the modified code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object) Dim prevSheet As Worksheet Dim sheetNames As Variant sheetNames = Array("PORDER", "WORKST", "WORKPR", "GRN") Set prevSheet = Worksheets(Me.Index - 1) ' Check if there is a selected cell in any sheet of the array Dim selectedCellAddress As Variant On Error Resume Next For Each ws In Worksheets(sheetNames) selectedCellAddress = ws.Range("A1").Address If Not IsError(ws.Range(selectedCellAddress).Select) Then Exit For End If Next ws On Error GoTo 0 ' If there is no selected cell in any sheet of the array, exit the sub If selectedCellAddress = "" Then Exit Sub End If ' Select the same cell as in the previous sheet If Not IsError(Application.Match(prevSheet.Name, sheetNames, 0)) Then ActiveSheet.Range(selectedCellAddress).Select End If ' Recalculate the worksheet Application.EnableEvents = False ActiveSheet.Calculate Application.EnableEvents = True End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.