I would like a VBA event named 'BackupOT' that is activated by a form button that does…
- User
I would like a VBA event named 'BackupOT' that is activated by a form button that does the following. In sheet 'Overtime' from A3, scroll down the column to the last entry and copy all rows from A to P and paste all cell values and the cell format without cell formulas into the next available row in sheet 'OTBackup' Then in sheet 'Overtime' clear the contents of the following columns A, C, D, E, F, I, K, M, O, P, AD, AE, AG, from row 3 to row 2003 and then calulate all the formlas in sheet 'Overtime'
- Assistant
Here is the VBA code for the 'BackupOT' event as you described:
Private Sub BackupOT_Click() Dim wsOvertime As Worksheet Dim wsOTBackup As Worksheet Dim lastRow As Long Dim nextRow As Long ' Set the worksheets Set wsOvertime = ThisWorkbook.Sheets("Overtime") Set wsOTBackup = ThisWorkbook.Sheets("OTBackup") ' Find the last used row in column A of 'Overtime' sheet lastRow = wsOvertime.Cells(wsOvertime.Rows.Count, "A").End(xlUp).Row ' Find the next available row in 'OTBackup' sheet nextRow = wsOTBackup.Cells(wsOTBackup.Rows.Count, "A").End(xlUp).Row + 1 ' Copy and paste values and formats from 'Overtime' to 'OTBackup' wsOvertime.Range("A3:P" & lastRow).Copy wsOTBackup.Cells(nextRow, "A").PasteSpecial xlPasteValuesAndNumberFormats ' Clear the specified columns in 'Overtime' wsOvertime.Range("A3:A2003,C3:F2003,I3,I3,M3,O3:P2003,AD3:AE2003,AG3").ClearContents ' Calculate all formulas in 'Overtime' Application.CalculateFull ' Clear the clipboard Application.CutCopyMode = False ' Activate the 'Overtime' sheet wsOvertime.Activate End Sub - User
Can I have a Yes No message that asks if I would like to Backup and Clear all Data. If I choose Yes then the Code contineues if No exit the sub
- Assistant
Certainly! Here's an updated version of the code that includes a Yes/No message box:
Private Sub BackupOT_Click() Dim wsOvertime As Worksheet Dim wsOTBackup As Worksheet Dim lastRow As Long Dim nextRow As Long Dim answer As VbMsgBoxResult ' Ask the user if they want to proceed with backup and clear operation answer = MsgBox("Are you sure you want to backup and clear all data?", vbQuestion + vbYesNo, "Backup and Clear Data") If answer = vbNo Then Exit Sub ' Exit the sub if user chooses No ' Set the worksheets Set wsOvertime = ThisWorkbook.Sheets("Overtime") Set wsOTBackup = ThisWorkbook.Sheets("OTBackup") ' Find the last used row in column A of 'Overtime' sheet lastRow = wsOvertime.Cells(wsOvertime.Rows.Count, "A").End(xlUp).Row ' Find the next available row in 'OTBackup' sheet nextRow = wsOTBackup.Cells(wsOTBackup.Rows.Count, "A").End(xlUp).Row + 1 ' Copy and paste values and formats from 'Overtime' to 'OTBackup' wsOvertime.Range("A3:P" & lastRow).Copy wsOTBackup.Cells(nextRow, "A").PasteSpecial xlPasteValuesAndNumberFormats ' Clear the specified columns in 'Overtime' wsOvertime.Range("A3:A2003,C3:F2003,I3,I3,M3,O3:P2003,AD3:AE2003,AG3").ClearContents ' Calculate all formulas in 'Overtime' Application.CalculateFull ' Clear the clipboard Application.CutCopyMode = False ' Activate the 'Overtime' sheet wsOvertime.Activate End Sub - User
How can I change this line of code to values only, no formats no formulas: wsOTBackup.Cells(nextRow, “A”).PasteSpecial xlPasteValuesAndNumberFormats
- Assistant
To paste values only without formats or formulas, you can modify the line of code as follows:
wsOTBackup.Cells(nextRow, "A").PasteSpecial xlPasteValuesThis will paste only the values from the clipboard to the destination range in the
OTBackupsheet. It won't include any formatting or formulas.