|
|
|
|
|
|
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()
Dim Result As Boolean
Dim Rng As Range
Dim ShtName As String
ShtName = ActiveSheet.Name
Set Rng = Selection
If Rng.Count = 1 Then Set Rng = ActiveSheet.UsedRange
Result = RemoveConditionalFormats(ShtName, Rng)
If Result = True Then
Else
End If
End Sub
Function RemoveConditionalFormats(Optional SheetName As String = "", _
Optional RangeToClear As Range = Nothing) _
As Boolean
Dim FCondition As FormatCondition
Dim Cel As Range
Dim i As Long
Dim Rng As Range
Dim Sht As Worksheet
RemoveConditionalFormats = False
On Error GoTo TheresAProblem
Application.ScreenUpdating = False
If SheetName = "" Then
Set Sht = ActiveSheet
Else
Set Sht = Sheets(SheetName)
End If
Set Rng = RangeToClear
If Rng Is Nothing Then
Set Rng = Sht.UsedRange
End If
For Each Cel In Rng
If Cel.FormatConditions.Count > 0 Then
For i = Cel.FormatConditions.Count To 1 Step -1
Cel.FormatConditions(i).Delete
Next i
End If
Next Cel
Application.ScreenUpdating = True
RemoveConditionalFormats = True
Exit Function
TheresAProblem:
Application.ScreenUpdating = True
RemoveConditionalFormats = False
End Function
|
How to use:
|
- Open an Excel workbook.
- Select Tools/Macro/Visual Basic Editor.
- In the VBE window, select Tools/Project Explorer.
- Right click "ThisWorkbook" in the left hand pane.
- In the PopUp menu, Put the Mouse over "Insert, and left click "Module."
- Copy the Code above.
- DoubleClick the new Module1 to Open it's CodePage.
- Paste the Code above into the Module1 CodePage.
- Now select File/Close and Return To Microsoft Excel
- Save the file.
- For WorkSheet Developers:
- To run the code on the Entire Sheet, Make sure that only one Cell is Selected.
- Hit Alt+F8.
- In the PopUp, select "RemoveConditionalFormats_Macro."
- Click "Run"
- To restrict the code to a selected area of the Sheet, make your selection of more than one Cell before hitting Alt +F8.
- For VBA Developers:
- To use the FUNCTION "RemoveConditionalFormats," See the MACRO "RemoveConditionalFormats_Macro"
|
Test the code:
|
- First:
- Generate some data in several areas of a Worksheet.
- Create some Conditional Formats for the different area's data.
- Then:
- Select at least two cells that include at least one Cell with at least one Conditional Format.
- Run the Macro as above.
- Only the selected cells should have their Conditional Formats removed.
- Finally:
- Select any one cell on the Worksheet.
- Run the Macro as above.
- 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.
|
|