Excel

Recycle Worksheets with Old or Wrong Entries

Ease of Use

Easy

Version tested with

2000 

Submitted by:

johnske

Description:

Clear just values, clear values AND code, or delete entire rows. 

Discussion:

You may have an old worksheet with elaborate headings and lots of conditional formatting and/or data validation that you want to preserve, but the many rows of entries are not wanted any longer, or the sheet may need to be re-coded. It can be time-consuming to copy and "paste-special" remaining data to a new sheet. There is still the problem of clearing the values and retaining the code if it's still wanted.... Running this procedure solves those problems and takes relatively little time.... 

Code:

instructions for use

			

Option Explicit Public StartClear As Integer Sub ClearDataBelowThisRow() Dim Question As Integer Dim Password, Msg, Answer, Confirm, MoreOptions As VbMsgBoxResult '//ask for input On Error GoTo LastLine Question = InputBox("What is the last row number you want to KEEP?", "Keep rows down to...") If Question <= 0 Then Exit Sub StartClear = Question + 1 ActiveSheet.Rows(StartClear & ":65536").Select '//give information Answer = MsgBox("Clear all entries from row " & StartClear & " down?", vbYesNo, "Cleaning out " & 65536 - StartClear & " Rows") If Answer = vbNo Then Range("A" & StartClear).Select Exit Sub End If '//request authority to delete Password = Application.InputBox(prompt:="Please enter password to confirm your" & _ "authority to take this action...", Title:="A Password Is Required For This Action...") '//replace 123 (below) with your own password, OR '//remove it by replacing "123" with "" or Empty If Password <> "123" Then WrongPassword Else '//proceed with clear ActiveSheet.Unprotect '//Clear or Delete? - confirm Confirm = MsgBox("Click YES to clear the selected region - (Leave the cell formulae," & vbLf & _ "cell formatting, conditional formatting and data validation intact)" & vbLf & _ "" & vbLf & _ "Click NO for other choices...", vbYesNoCancel, "Confirm Details...") Application.ScreenUpdating = False '//clear formulae only If Confirm = vbYes Then On Error Resume Next '// "error" here is: nothing to clear Selection.SpecialCells(xlCellTypeConstants, 23).ClearContents Range("A" & StartClear).Select Exit Sub End If '//if user chooses no - more details If Confirm = vbNo Then MoreOptions = MsgBox("Click YES to clear the contents AND the formulae from the selected region." & vbLf & _ "(Leave the cell formatting, conditional formatting and data validation intact)" & vbLf & _ "" & vbLf & _ "Click NO to Delete absolutely everything...", vbYesNoCancel, "CHOOSE CAREFULLY !") '//normal clear contents If MoreOptions = vbYes Then On Error Resume Next '// "error" here is: nothing to clear Selection.ClearContents Range("A" & StartClear).Select Exit Sub End If '//DELETE selection If MoreOptions = vbNo Then On Error Resume Next '// will not happen Selection.Delete Range("A" & StartClear).Select Exit Sub End If '//cancel If MoreOptions = vbCancel Then Range("A" & StartClear).Select Exit Sub End If '//cancel If Confirm = vbCancel Then Range("A" & StartClear).Select Exit Sub End If Exit Sub End If LastLine: '//an non-integer was entered for 'Question' so quit the sub End Sub '************************************************************* ' Error message - Wrong password was given for "ClearDataBelowThisRow" '************************************************************* Private Sub WrongPassword() Dim Msg As VbMsgBoxResult Range("A" & StartClear).Select Msg = MsgBox("Denied : - That is not the password !", vbExclamation, "Incorrect Password") End End Sub

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Insert/Module
  4. Copy and paste the code into the Module
  5. Now select File/Close and Return To Microsoft Excel
  6. Select Tools/Macro/Macros.../ClearDataBelowThisRow/Run
  7. Now follow the prompts
  8. (Note that the password needed is 123) - Change it to suit yourself
 

Test the code:

  1. Use the code on a copy of a workbook that has lots of entries, conditional formatting, etc.
  2. Select Tools/Macro/Macros.../ClearDataBelowThisRow/Run
  3. Now follow the prompts
  4. An example to practice and experiment with is included in the zip attachment
  5. Note: To prevent unwanted or malicious use of this procedure by others, go back into the VBE window
  6. Select Tools/VBA Project Properties.../Protection and password protect your coded password
 

Sample File:

Clear Rows.zip 208.02KB 

Approved by mdmackillop


This entry has been viewed 87 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express