PC Review


Reply
Thread Tools Rate Thread

Delete single conditional format

 
 
Ron Rosenfeld
Guest
Posts: n/a
 
      11th Jul 2009
Excel 2007 / VBA 6.5

Is it possible to delete a single conditional format? (My goal is to delete
all except 2.

I have tried to do so (code below) and receive a 1004 error.

When recording a macro while deleting two of the formats, it appears to delete
ALL of the formats, and then recreate the formats that remain.

Here is the code I tried:

=============================
Private Sub Worksheet_Activate()
Dim i As Long

With Worksheets("solver").Cells
If .FormatConditions.Count > 2 Then
For i = 1 To .FormatConditions.Count - 2
.FormatConditions(i).Delete
Next i
End If
End With
End Sub
===========================
--ron
 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      11th Jul 2009
It might depend on the particular CFs as to whether you can delete them
individually. However, you won't do it with the code as posted, have a go
with something like this

Sub test()
Dim i As Long
Dim c As Range, r As Range

On Error Resume Next
Set r =
Worksheets("sheet1").Cells.SpecialCells(xlCellTypeAllFormatConditions)
On Error GoTo errExit

If Not r Is Nothing Then
For Each c In r
With c.FormatConditions
If .Count > 2 Then
For i = .Count To 3 Step -1
.Item(i).Delete
Next
End If
End With
Next
End If
Exit Sub

errExit:
' for debugging only
c.Select
Debug.Print c.Address
Stop
Resume Next

End Sub

Regards,
Peter T

"Ron Rosenfeld" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Excel 2007 / VBA 6.5
>
> Is it possible to delete a single conditional format? (My goal is to
> delete
> all except 2.
>
> I have tried to do so (code below) and receive a 1004 error.
>
> When recording a macro while deleting two of the formats, it appears to
> delete
> ALL of the formats, and then recreate the formats that remain.
>
> Here is the code I tried:
>
> =============================
> Private Sub Worksheet_Activate()
> Dim i As Long
>
> With Worksheets("solver").Cells
> If .FormatConditions.Count > 2 Then
> For i = 1 To .FormatConditions.Count - 2
> .FormatConditions(i).Delete
> Next i
> End If
> End With
> End Sub
> ===========================
> --ron



 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      11th Jul 2009
On Sat, 11 Jul 2009 13:42:12 +0100, "Peter T" <peter_t@discussions> wrote:

>It might depend on the particular CFs as to whether you can delete them
>individually. However, you won't do it with the code as posted, have a go
>with something like this
>
>Sub test()
>Dim i As Long
>Dim c As Range, r As Range
>
> On Error Resume Next
> Set r =
>Worksheets("sheet1").Cells.SpecialCells(xlCellTypeAllFormatConditions)
> On Error GoTo errExit
>
> If Not r Is Nothing Then
> For Each c In r
> With c.FormatConditions
> If .Count > 2 Then
> For i = .Count To 3 Step -1
> .Item(i).Delete
> Next
> End If
> End With
> Next
> End If
> Exit Sub
>
>errExit:
>' for debugging only
> c.Select
> Debug.Print c.Address
> Stop
> Resume Next
>
>End Sub
>
>Regards,
>Peter T
>


Thanks, Peter.

I'll give it a try.
--ron
 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      13th Jul 2009
On Sat, 11 Jul 2009 13:42:12 +0100, "Peter T" <peter_t@discussions> wrote:

>It might depend on the particular CFs as to whether you can delete them
>individually. However, you won't do it with the code as posted, have a go
>with something like this
>
>Sub test()
>Dim i As Long
>Dim c As Range, r As Range
>
> On Error Resume Next
> Set r =
>Worksheets("sheet1").Cells.SpecialCells(xlCellTypeAllFormatConditions)
> On Error GoTo errExit
>
> If Not r Is Nothing Then
> For Each c In r
> With c.FormatConditions
> If .Count > 2 Then
> For i = .Count To 3 Step -1
> .Item(i).Delete
> Next
> End If
> End With
> Next
> End If
> Exit Sub
>
>errExit:
>' for debugging only
> c.Select
> Debug.Print c.Address
> Stop
> Resume Next
>
>End Sub
>
>Regards,
>Peter T
>


Well, a variation of what you posted is working perfectly OK. I'm guessing a
pertinent point to what I missed is that I can't delete individual CF's unless
I loop through each cell (although I can delete them all).

Thanks for your help.
--ron
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditional format of entire row based on a single cell in row CEG_Staffer Microsoft Excel Worksheet Functions 7 16th Jul 2009 10:54 PM
Conditional Format overwrighting previous conditional format davethewelder Microsoft Excel Programming 2 10th Apr 2008 04:01 PM
Setting an Icon Set conditional format for a single cell??? MikeV Microsoft Excel Misc 4 14th Feb 2008 12:20 AM
Setting an Icon Set conditional format for a single cell??? velasquez.m@gmail.com Microsoft Excel Discussion 1 13th Feb 2008 05:12 PM
Conditional Format to Delete Row =?Utf-8?B?VGVyZXNhIEM=?= Microsoft Excel Programming 2 24th Oct 2005 08:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:16 PM.