Excel

Excel Conditional Formats

Ease of Use

Easy

Version tested with

2002 

Submitted by:

SamT

Description:

Will Remove all Conditional Formats from a Desired Worksheet and Range. 

Discussion:

I was developing a report sheet on some unfamiliar Raw Data. I had Conditionally Formatted the data. When I copied and pasted the data into the working Sheet, I missed a Column and all my Formulas showed massive errors. I then correctly copied and pasted, which would up mixing the Conditional Formats of both paste operations. Rather than manually removing each CF from the approximate 1500 cells, I wrote this to do it "Macromatically", because I couldn't find anything in the KB. After completing the report sheet, I polished the original code into a Function that can be called by VBA code to remove Conditional Formatting from any Sheet and any Range on the Sheet. I added a small Sub type Procedure so that the main function can still be called by using the Excel Menu >> Tools >> Macros. The Sub Procedure only works on the Active Sheet and will remove Conditional Formats from any Selection of more than one Cell, or the Entire Sheet if only one Cell is selected when the Macro is run. For Worksheet developers, this provides a simple way to remove all Conditional formats from a Range. For VBA Developers, it provides a callable Function to do the same for any Worksheet and any Range on that Worksheet. The Macro for Worksheet Developers does not return any error information. The Function For VBA returns a True if no errors, or a False. It is expected that almost all errors will be because of improper Calls by the Coder. Lack of ConditionalFormats does not generate an Error. 

Code:

instructions for use

			

Option Explicit Sub RemoveConditionalFormats_Macro() 'The Macro is to provide Menu access to the main function. 'It is also the example for the main function's usage ' ' As a Macro, it assumes that IF the User has NOT selected more ' than one Cell, THEN the User wants to clear the entire active sheet. ' 'In-Code comments apply to the Usage of the main function Dim Result As Boolean Dim Rng As Range Dim ShtName As String ''''Assign a Sheet to use ShtName = ActiveSheet.Name ' Alternately 'ShtName = "SomeSheetName" ''''Assign a Range to clear Set Rng = Selection ' Alternately: 'Set Rng = Range("A1:B25") ' Alternately: 'Set Rng = Code that returns a Range 'For this macro only If Rng.Count = 1 Then Set Rng = ActiveSheet.UsedRange ''''Call the function 'If no parameters were supplied, the main function would clear ' the entire active sheet. Result = RemoveConditionalFormats(ShtName, Rng) ''''Test For success If Result = True Then 'Either found no FormatConditions or successfully removed all of them Else 'Problem, check your code. End If End Sub Function RemoveConditionalFormats(Optional SheetName As String = "", _ Optional RangeToClear As Range = Nothing) _ As Boolean 'Will return FALSE on any error. 'Returns TRUE if no FormatConditions found or if all desired were ' successfully removed. 'The Default is to clear the entire ActiveSheet 'The Coder can specify which sheet and Cell(s) to clear. ' See Sub RemoveConditionalFormats_Macro above for example of usage. Dim FCondition As FormatCondition Dim Cel As Range Dim i As Long Dim Rng As Range Dim Sht As Worksheet ''''Insure proper Initialization. Always Be Sure, (ABS Motto!) 'ABS is not recommended where speed is crucial. RemoveConditionalFormats = False ''''Minimal Error Handling. On Error GoTo TheresAProblem ''''Turn off screen updates to improve performance Application.ScreenUpdating = False ''''Determine Worksheet and Range to use. If SheetName = "" Then 'The SheetName arg was not supplied Set Sht = ActiveSheet Else 'Use the arg as supplied Set Sht = Sheets(SheetName) End If Set Rng = RangeToClear If Rng Is Nothing Then 'The RangeToClear arg was not supplied Set Rng = Sht.UsedRange ' therefore, clear the Entire sheet End If ''''Remove each FormatCondition Object from each Cell in the desired Range. For Each Cel In Rng 'Check if the Cell has any FormatCondition Objects. If Cel.FormatConditions.Count > 0 Then 'Index backwards because Collection.Count changes with each deletion. (ABS) For i = Cel.FormatConditions.Count To 1 Step -1 Cel.FormatConditions(i).Delete Next i End If Next Cel ''''Operation finished with no problems ''''Turn screen updates back on Application.ScreenUpdating = True ''''Set the function's return value to show success RemoveConditionalFormats = True Exit Function TheresAProblem: ''''Turn screen updates back on. (ABS) Application.ScreenUpdating = True ''''An Error occured. Set the Function's return value to False. 'Leave some clue for the coder, do NOT Zero out the Error. RemoveConditionalFormats = False End Function

How to use:

  1. Open an Excel workbook.
  2. Select Tools/Macro/Visual Basic Editor.
  3. In the VBE window, select Tools/Project Explorer.
  4. Right click "ThisWorkbook" in the left hand pane.
  5. In the PopUp menu, Put the Mouse over "Insert, and left click "Module."
  6. Copy the Code above.
  7. DoubleClick the new Module1 to Open it's CodePage.
  8. Paste the Code above into the Module1 CodePage.
  9. Now select File/Close and Return To Microsoft Excel
  10. Save the file.
  11. For WorkSheet Developers:
  12. To run the code on the Entire Sheet, Make sure that only one Cell is Selected.
  13. Hit Alt+F8.
  14. In the PopUp, select "RemoveConditionalFormats_Macro."
  15. Click "Run"
  16. To restrict the code to a selected area of the Sheet, make your selection of more than one Cell before hitting Alt +F8.
  17. For VBA Developers:
  18. To use the FUNCTION "RemoveConditionalFormats," See the MACRO "RemoveConditionalFormats_Macro"
 

Test the code:

  1. First:
  2. Generate some data in several areas of a Worksheet.
  3. Create some Conditional Formats for the different area's data.
  4. Then:
  5. Select at least two cells that include at least one Cell with at least one Conditional Format.
  6. Run the Macro as above.
  7. Only the selected cells should have their Conditional Formats removed.
  8. Finally:
  9. Select any one cell on the Worksheet.
  10. Run the Macro as above.
  11. All Conditional Formats should be removed from all cells on the Worksheet.
 

Sample File:

RemoveConditionalFormats.zip 12.62KB 

Approved by mdmackillop


This entry has been viewed 103 times.

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