I need a vba code that will clear the values in A2 to F2 in the Sheet Job Request when I…
- User
I need a vba code that will clear the values in A2 to F2 in the Sheet Job Request when I select the sheet and also when I leave the sheet
- Assistant
Here is the VBA code that will clear the values in A2 to F2 in the "Job Request" sheet when you select or leave the sheet:
Private Sub Worksheet_Activate() Range("A2:F2").ClearContents End Sub
Private Sub Worksheet_Deactivate() Range("A2:F2").ClearContents End Sub
You need to add this code to the code module of the "Job Request" sheet. To do that, follow these steps:
- Right-click on the sheet tab of the "Job Request" sheet.
- Select "View Code" from the context menu. This will open the code module for the sheet.
- Paste the above code into the module.
- Save the workbook.
- User
Ihave an excel workbook with about 50 different sheets. The workbook has many formulas and vba codes that all work as expected without errors. However, when I open the workbook it takes ages for the start sheet to become active due to calculations. In all the sheets, when I enter a value in a cell, it also takes a while before the sheet allows me to move to another cell. Are there suggestions to reduce this delay.
- Assistant
Here are some suggestions to reduce the delay in your Excel workbook:
-
Turn off automatic calculation: By default, Excel recalculates all formulas in the workbook each time a cell value changes. This can slow down your workbook, especially if you have complex formulas or large amounts of data. You can turn off automatic calculation by going to the "Formulas" tab, selecting "Calculation Options", and choosing "Manual". This way, you can choose when to recalculate the formulas in the workbook.
-
- User
what will be a vba code to turn off automatic calculation when the workbook is opened and revert back to automatic calculation when the workbook is closed. I would also want a code to claculate a sheet when the sheet is opened
- Assistant
Here is the VBA code that will turn off automatic calculation when the workbook is opened, calculate a sheet when the sheet is opened, and revert back to automatic calculation when the workbook is closed:
Private Sub Workbook_Open() Application.Calculation = xlManual End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean) Application.Calculation = xlAutomatic End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object) Sh.Calculate End Sub
You need to add this code to the code module of the workbook. To do that, follow these steps:
- Press ALT + F11 to open the VBA editor.
- In the Project Explorer, find and double-click on the "ThisWorkbook" object to open its code module.
- Paste the above code into the module.
- Save the workbook.
The "Workbook_Open" event will turn off automatic calculation when the workbook is opened. The "Workbook_BeforeClose" event will revert back to automatic calculation when the workbook is closed. The "Workbook_SheetActivate" event will calculate the active sheet when you switch to it.