In excel, using a form button in Sheet Job Request, I want to call a macro to first…
- User
In excel, using a form button in Sheet Job Request, I want to call a macro to first calculate Sheet Job Request, then 4 seconds later copy the last 10 cells with values fom Column J of the Sheet named in I2 of the sheet Job Request, and paste the copied values into Column E starting from E5 of the Sheet Request Sheet. If the values found in Column J of the Sheet named in I2 of the sheet Job Request are less than ten rows, the macro must only copy up to row 4 and paste the copied values into Column E starting from E5 of the Sheet Request Sheet.
- Assistant
Here is the code for the macro:
Sub CopyLast10Cells()
'Replace Sheet1 with the name of your Job Request sheet 'Replace Sheet2 with the name of your Request sheet 'Replace I2 with the cell reference containing the name of the sheet to be copied from
Dim sourceSheet As Worksheet Dim destSheet As Worksheet Dim lastRow As Long Dim copyRange As Range
Set sourceSheet = Sheets(Range("I2")) Set destSheet = Sheets("Request Sheet")
Application.ScreenUpdating = False
'Set the calculation mode to manual and recalculate the source sheet Application.Calculation = xlCalculationManual sourceSheet.Calculate
'Wait for 4 seconds Application.Wait (Now + TimeValue("0:00:04"))
'Find the last non-empty row in the source sheet column J lastRow = sourceSheet.Cells(Rows.Count, "J").End(xlUp).Row
'Set the range to be copied If lastRow > 10 Then Set copyRange = sourceSheet.Range("J" & lastRow - 9 & ":J" & lastRow) Else Set copyRange = sourceSheet.Range("J4:J" & lastRow) End If
- User
In excel when I leave a sheet, I would like to clear the contents of E5 to E20
- Assistant
Here is the VBA code you can use to clear the contents of cells E5:E20 in a sheet when you leave it:
-
Right-click on the sheet tab and click "View Code" to open the Visual Basic Editor.
-
Paste the following code:
codePrivate Sub Worksheet_Deactivate() Range("E5:E20").ClearContents End Sub- Save the workbook and test the code by switching to another sheet. The contents of cells E5:E20 should automatically be cleared.
Note: This code should be added to the specific sheet's code module that you want it to apply to. If you want this code to apply to multiple sheets, you will need to add it to each sheet's code module.
-