ExcelHome技术论坛

标题: [分享]VBA实现单元格条件格式的属性、方法 [打印本页]

作者: Aeolian-Vox    时间: 2006-11-30 18:02
标题: [分享]VBA实现单元格条件格式的属性、方法

查阅了不少的关于单元格条件格式的VBA方面的资料,多数问题所涉及到的是用代码设置或者修改单元格已有的条件格式,用代码获取已有的单元格条件格式所对应的属性案例是少之又少,较为全面的资料和实际复杂案例的介绍,几乎可以说是凤毛麟角的了。

本帖把收集到的单元格条件格式资料进行了分类、整理、汇总,分享于有所需求的各位朋友。如有不妥之处,务必指正,以共勉。

本帖分享的单元格条件格式内容:

1、  单元格条件格式在VBA中的描述

2、  单元格条件格式的属性

3、  单元格条件格式可用的格式单元格的显示属性

4、  单元格条件格式的方法

5、  VBA代码操作单元格条件格式

6、  VBA代码快速定位含条件格式的单元格

7、  VBA代码取得条件格式属性

8、  VBA代码转化条件格式为真的属性值为单元格的属性值

 

1、单元格条件格式在VBA的中描述

FormatCondition 对象

代表一个条件格式。FormatCondition 对象是 FormatConditions 集合的成员。FormatConditions 集合最多可包含给定区域的三个条件格式。

(备注:FormatConditions 是指某一个单元格中的条件格式的集合,并非所有单元格的条件格式集合)


作者: Aeolian-Vox    时间: 2006-11-30 18:06

2、单元格条件格式的属性

3、单元格条件格式可用的格式单元格显示属性

可用 FormatCondition 对象的 FontBorder Interior 属性控制已设定格式单元格的显示。条件格式对象模型不支持这些对象的某些属性。下表中列出所有可使用条件格式的属性。

对象

属性

Font

Bold

Color

ColorIndex

FontStyle

Italic

Strikethrough

Underline

无法使用会计下划线样式。

Border

Bottom

Color

Left

Right

Style

可使用下列边框样式(其他均不可用):xlNonexlSolidxlDashxlDotxlDashDotxlDashDotDotxlGray50xlGray75 xlGray25

Top

Weight

可使用下列边框粗细(其他均不可用):xlWeightHairline xlWeightThin

Interior

Color

ColorIndex

Pattern

PatternColorIndex

[em05]
作者: Aeolian-Vox    时间: 2006-11-30 18:08

4、单元格条件格式的方法

1、 Add方法:

添加新的条件格式。返回 FormatCondition 对象,该对象代表新添加的条件格式。

expression.Add(Type, Operator, Formula1, Formula2)

expression      必需。该表达式返回一个 FormatConditions 对象。

Type      XlFormatConditionType 类型,必需。指定条件格式是基于单元格值,还是基于表达式。

XlFormatConditionType 可为以下 XlFormatConditionType 常量之一。

xlCellValue 基于单元格值的条件格式。

xlExpression 基于表达式的条件格式。

Operator      Variant 类型,可选。条件格式运算符。可为以下 XlFormatConditionOperator 常量之一:xlBetweenxlEqualxlGreaterxlGreaterEqualxlLessxlLessEqualxlNotBetween xlNotEqual。如果 Type xlExpression,则忽略 Operator 参数。

Formula1      Variant 类型,可选。与条件格式相关的表达式或数值。可为常量、字符串、单元格引用或公式。

Formula2      Variant 类型,可选。当 Operator xlBetween xlNotBetween 时,为与条件格式第二部分相关的表达式或数值(否则,则忽略本参数)。可为常量、字符串、单元格引用或公式。

 

[em05]
作者: Aeolian-Vox    时间: 2006-11-30 18:15

1、 Delete方法

更改现有条件格式。

expression.Modify(Type, Operator, Formula1, Formula2)

expression      必需。该表达式返回一个 FormatCondition 对象。

Type     XlFormatCondition 类型,必需。指定条件格式是基于单元格值还是基于表达式。

XlFormatCondition 可为以下 XlFormatCondition 常量之一。

xlCellValue

xlExpression

Operator     XlFormatConditionOperator 类型,可选。条件格式操作符。

XlFormatConditionOperator 可为以下 XlFormatConditionOperator 常量之一。

xlBetween

xlEqual

xlGreater

xlGreaterEqual

xlLess

xlLessEqual

xlNotBetween

xlNotEqual

如果 Type xlExpression,则忽略 Operator 参数。

Formula1      Variant 类型,可选。与条件格式相联系的表达式或数值。可为常量、字符串、单元格引用或公式。

Formula2      Variant 类型,可选。与条件格式相联系的表达式或数值。可为常量、字符串、单元格引用或公式。

Add 方法新建一个条件格式时,如果试图为单个区域创建三个以上的条件格式,则 Add 方法失效。如果一个区域有三个格式,请使用 Modify 方法对这些格式进行修改,或者使用 Delete 方法删除某个格式,然后使用 Add 方法新建一个格式。


作者: Aeolian-Vox    时间: 2006-11-30 18:24

5、用VBA代码操作单元格条件格式

A、增加条件格式

'本示例向单元格区域 B1: B5 中添加条件格式

Sub Add_FormatCondition()

Sheet1.Range("B1:B5").FormatConditions.Delete

With Sheet1.Range("B1:B5").FormatConditions.Add(xlCellValue, xlGreater, "=$a$1")

    With .Borders

        .LineStyle = xlContinuous

        .Weight = xlThin

        .ColorIndex = 6

    End With

    With .Font

        .Bold = True

        .ColorIndex = 3

    End With

End With

End Sub

效果如下图演示:

 


[em05]
作者: Aeolian-Vox    时间: 2006-11-30 18:25

另外:一个复杂的设置条件格式的代码,包括单元格字体颜色、单元格边框、单元格底色、单元格图案。

Sub Set_FormatConditions()

'设置条件格式2006-11-24

    Dim i%, n%, m%

    Dim Rng_Format_Format As FormatConditions

    Application.ScreenUpdating = False

    Set Rng_Format = Range("E1:E10").FormatConditions

    Rng_Format.Delete

    Rng_Format.Add Type:=xlExpression, Formula1:="=$G$5=5"

    With Rng_Format(1).Font

        .ColorIndex = 3

    End With

    m = 1

    For i = -4160 To -4107

        Select Case m

        Case 1

            n = 0

        Case 2

            n = 7

        Case 3

            n = 20

        Case 4

            n = 23

        Case Else

            Exit For

        End Select

        i = i + n

        m = m + 1

        '设置单元格条件格式成立,边框样式

        With Rng_Format(1).Borders(i)

            .LineStyle = xlContinuous

            .Weight = xlThin

            .ColorIndex = 7

        End With

    Next

    '设置单元格条件格式成立,单元格内部样式

    With Rng_Format(1).Interior

        .ColorIndex = 35    '单元格底色

        .PatternColorIndex = 5    '单元格内部图案色

        .Pattern = xlLightHorizontal    '单元格图案样式

    End With

    Application.ScreenUpdating = True

End Sub

[em05]
作者: Aeolian-Vox    时间: 2006-11-30 18:33

B、修改条件格式

'本示例更改单元格区域 B1: B5 的现有条件格式

Sub Modify_FormatCondition()

On Error Resume Next '避免没有条件格式的单元格

    Sheet1.Range("B1:B5").FormatConditions(1).Modify xlCellValue, xlLess, "=$a$1"

End Sub

效果如下图演示,注意第一个演示中的条件为大于,修改后的为小于:

 

C、  删除条件格式

Sub Del_FormatConditions()

'删除全部单元格的条件格式

On Error Resume Next '避免没有条件格式的单元格

    ActiveCell.SpecialCells(xlCellTypeAllFormatConditions).Delete

可以删除指定某一个区域单元格的条件格式

End Sub


[em05]
作者: Aeolian-Vox    时间: 2006-11-30 18:45

6、用VBA代码快速定位含条件格式的单元格

l         选择全部含条件格式的单元格

Sub Select_All_FormatConditions()

On Error Resume Next '避免没有条件格式的单元格

    Cells.SpecialCells(xlCellTypeAllFormatConditions).Select

End Sub

l         选择指定区域含条件格式的单元格

Sub Select_Part_FormatConditions()

On Error Resume Next '避免没有条件格式的单元格

    'Columns(2).SpecialCells(xlCellTypeAllFormatConditions).Select

    Range("E1:E10").SpecialCells(xlCellTypeAllFormatConditions).Select

End Sub

l         选择指定区域含相同条件格式的单元格

Sub Select__Sameness_FormatConditions()

'以最前面的区域,做为优先选择

On Error Resume Next '避免没有条件格式的单元格

    Range("B1:F15").SpecialCells(xlCellTypeSameFormatConditions).Select

End Sub

  以上方法定位得到的区域,可以用对定义的单元格对象变量进行赋值操作:

Sub Set_FormatConditions_Evaluate()

'条件格式对象变量赋值

On Error Resume Next '避免没有条件格式的单元格

    Dim Rng As Range

    Set Rng = Cells.SpecialCells(xlCellTypeAllFormatConditions)

End Sub

效果如下图演示:


[em05]
作者: Aeolian-Vox    时间: 2006-11-30 18:47
7、用VBA代码取得条件格式属性
'请在立即窗口下验证结果。
Sub Gain_FormatConditions_Setting() '获取条件格式的相关条件。注意容错处理
    On Error Resume Next
    Dim Rng As Range, t_Rng As Range
    Set Rng = Cells.SpecialCells(xlCellTypeAllFormatConditions)
    For Each t_Rng In Rng
        '获取条件格式1中的表达式类型
        Debug.Print t_Rng.FormatConditions(1).Type
        '获取条件格式1中的表达式中的操作符类型
        Debug.Print t_Rng.FormatConditions(1).Operator
        '1为单元格值;2为单元格公式
        '返回条件格式1中的条件1的表达式1字串
        Debug.Print t_Rng.FormatConditions(1).Formula1
        '返回条件格式1中的条件1中的表达式2字串,
        Debug.Print t_Rng.FormatConditions(1).Formula2
        '返回条件格式1中的单元格字体色
        Debug.Print t_Rng.FormatConditions(1).Font.ColorIndex
        '返回条件格式1中的单元格填充色
        Debug.Print t_Rng.FormatConditions(1).Interior.ColorIndex
        '返回条件格式1中的单元格图案色
        Debug.Print t_Rng.FormatConditions(1).Interior.PatternColorIndex
        '返回条件格式1中的单元格图案样式索引
        Debug.Print t_Rng.FormatConditions(1).Interior.Pattern
        '返回条件格式1中的字体属性信息
        With t_Rng.FormatConditions(1).Font
            Debug.Print .Bold    '加粗
            Debug.Print .Italic    '斜体
            Debug.Print .Underline    '下划线
            Debug.Print .Strikethrough    '删除线
        End With
        '返回条件格式1中的单元格边框左边框线信息
        With t_Rng.FormatConditions(1).Borders(-4131)
            Debug.Print .LineStyle    '线条样式
            Debug.Print .Weight    '线条宽度
            Debug.Print .ColorIndex    '线条颜色
        End With
        '返回条件格式1中的单元格边框右边框线信息
        With t_Rng.FormatConditions(1).Borders(-4152)
            Debug.Print .LineStyle    '线条样式
            Debug.Print .Weight    '线条宽度
            Debug.Print .ColorIndex    '线条颜色
        End With
        '返回条件格式1中的单元格边框上边框线信息
        With t_Rng.FormatConditions(1).Borders(-4160)
            Debug.Print .LineStyle    '线条样式
            Debug.Print .Weight    '线条宽度
            Debug.Print .ColorIndex    '线条颜色
        End With
        '返回条件格式1中的单元格边框下边框线信息
        With t_Rng.FormatConditions(1).Borders(-4131)
            Debug.Print .LineStyle    '线条样式
            Debug.Print .Weight    '线条宽度
            Debug.Print .ColorIndex    '线条颜色
        End With
    Next
End Sub

作者: Aeolian-Vox    时间: 2006-11-30 19:02

8、用VBA代码转化条件格式为真的属性值为单元格的属性值
Sub Hold_FormatConditions_Result()
'转化条件格式成立,保留单元格条件格式属性的结果
'1、单元格内部颜色属性
'2、单元格字体属性
'3、单元格边框样式属性
'4、单元格底纹样式属性
On Error Resume Next '避免没有条件格式的单元格
 Application.ScreenUpdating = False
    Dim s_Operator(8) '存放操作符的数组
    Dim Rng As Range, t_Rng As Range
    Dim t_Rng_Val '含条件格式单元格的值
    Dim Operator_sTr% '操作符类型对应的序号
    Dim V_Fc_1, V_Fc_2 '表达式1、2中的结果
    Dim t_V_Fc_a, t_V_Fc_b '临时变量
    Dim s_Strs, s_Str '操作符
    Dim ans As Boolean '判断条件成立与否的变量
    Dim Con%, n%, i%
    Dim s1 As Object '条件格式中的单元格字体
    Dim s2 As Object '条件格式中的单元格内部
    Dim s3 As Object '条件格式中的单元格边框
    s_Operator(1) = "=And(vCell>=For1,vCell<=For2)"    'Between
    s_Operator(2) = "=Not(And(vCell>=For1,vCell<=For2))"       'NotBetween
    s_Operator(3) = "=vCell=For1"               '=
    s_Operator(4) = "=vCell<>For1"              '<>
    s_Operator(5) = "=vCell>For1"               '>
    s_Operator(6) = "=vCell<For1"               '<
    s_Operator(7) = "=vCell>=For1"              '>=
    s_Operator(8) = "=vCell<=For1"              '<=
    Set Rng = Cells.SpecialCells(xlCellTypeAllFormatConditions)
    For Each t_Rng In Rng
        n = t_Rng.FormatConditions.Count '获取含单元格的条件格式总数
        If n > 0 Then
            Con = 0
            For i = n To 1 Step -1
                With t_Rng
                    t_Rng.Select '此语句是为了调试方便留下的,可以根据情况删除
                    If .FormatConditions(i).Type = 1 Then '条件单元格为值类型
                        t_Rng_Val = t_Rng.Value '取得含条件格式单元格的值
                        Operator_sTr = .FormatConditions(i).Operator '返回该条件格式的操作符
                        '返回该条件格式中的条件表达式1
                        V_Fc_1 = Application.Evaluate(.FormatConditions(i).Formula1)
                        '操作符为介于或者不介于
                        If Operator_sTr = 1 Or Operator_sTr = 2 Then
                         '返回该条件格式中的条件表达式2
                            V_Fc_2 = Application.Evaluate(.FormatConditions(i).Formula2)
                            '单元格值、条件格式表达1的值、条件格式表达2的值是不为数值类型
                            If Not (IsNumeric(t_Rng_Val)) Or Not (IsNumeric(V_Fc_1)) Or Not (IsNumeric(V_Fc_2)) Then
                                '为空值,则转换为 "" 类型
                                If IsEmpty(t_Rng_Val) Then t_Rng_Val = ""
                                '为数值,则转换为字符类型
                                If IsNumeric(t_Rng_Val) Then t_Rng_Val = CStr(t_Rng_Val)
                                '表达式1为空值,则转换为 "" 类型
                                If IsEmpty(V_Fc_1) Then V_Fc_1 = ""
                                '表达式1为数值,则转换为字符类型
                                If IsNumeric(V_Fc_1) Then V_Fc_1 = CStr(V_Fc_1)
                                '表达式2为空值,则转换为 "" 类型
                                If IsEmpty(V_Fc_2) Then V_Fc_2 = ""
                                '表达式2为空值,则转换为字符类型
                                If IsNumeric(V_Fc_2) Then V_Fc_2 = CStr(V_Fc_2)
                            Else
                                If IsEmpty(t_Rng_Val) Then t_Rng_Val = 0
                                If IsEmpty(V_Fc_1) Then V_Fc_1 = 0
                                If IsEmpty(V_Fc_2) Then V_Fc_2 = 0
                            End If


作者: Aeolian-Vox    时间: 2006-11-30 19:16
标题: [分享]VBA实现单元格条件格式的属性、方法

'接上贴

                            '表达式1、表达式2的比较
                            If V_Fc_1 > V_Fc_2 Then
                                t_V_Fc_a = V_Fc_2
                                t_V_Fc_b = V_Fc_1
                            Else
                                t_V_Fc_a = V_Fc_1
                                t_V_Fc_b = V_Fc_2
                            End If
                        Else '操作符序号大于2的情况
                            t_V_Fc_a = V_Fc_1
                            If t_Rng_Val < V_Fc_1 Then
                            '单元格值小于条件格式的设置的值,即条件成立的情况
                                ans = True
                                Con = i
                                Exit For
                            End If
                        End If
                        '单元格值、条件格式表达式1、条件格式表达式2的返回值:为文本时,将小写英文字符转换为大写英文字符
                        If Application.WorksheetFunction.IsText(t_Rng_Val) Then t_Rng_Val = """" & UCase(t_Rng_Val) & """"
                        If Application.WorksheetFunction.IsText(t_V_Fc_a) Then t_V_Fc_a = """" & UCase(t_V_Fc_a) & """"
                        If Application.WorksheetFunction.IsText(t_V_Fc_b) Then t_V_Fc_b = """" & UCase(t_V_Fc_b) & """"
                        '返回s_Str字符串中的操作符为:可转换一个对象或者一个值做替换操作
                        s_Strs = s_Operator(Operator_sTr)
                        s_Str = Replace(s_Strs, "For1", t_V_Fc_a)
                        s_Str = Replace(s_Str, "For2", t_V_Fc_b)
                        s_Str = Replace(s_Str, "vCell", t_Rng_Val)
                        '将s_Str 转换为值出现错误时
                        If Application.WorksheetFunction.IsError(Application.Evaluate(s_Str)) Then
                        Else '转换成功。则条件格式成立
                            ans = Application.Evaluate(s_Str)
                        End If

                    Else '条件格式为公式
                        If Application.WorksheetFunction.IsError(Application.Evaluate(.FormatConditions(i).Formula1)) Then
                        Else
                            ans = Application.Evaluate(.FormatConditions(i).Formula1)
                            Con = i
                        End If
                    End If
                End With
            Next
            If Con > 0 Then
                Set s1 = t_Rng.FormatConditions(Con).Font '条件格式中设置的字体
                Set s2 = t_Rng.FormatConditions(Con).Interior '条件格式中设置的单元格内部
                Set s3 = t_Rng.FormatConditions(Con).Borders '条件格式中设置的单元格边框
                With t_Rng.Font '条件格式成立的单元格字体
                    .Bold = s1.Bold '加粗
                    .Italic = s1.Italic '斜体
                    .Underline = s1.Underline '下划线
                    .Strikethrough = s1.Strikethrough '删除线
                    .ColorIndex = s1.ColorIndex '字体颜色索引号
                End With
                With t_Rng
                    .Interior.ColorIndex = s2.ColorIndex  '单元格内部颜色索引号
                    .Interior.Pattern = s2.Pattern '单元格内部图案
                    .Interior.PatternColorIndex = s2.PatternColorIndex '单元格内部图案颜色索引号
                    .Borders.LineStyle = s3.LineStyle '单元格边框线类型
                    .Borders.ColorIndex = s3.ColorIndex '单元格边框线颜色索引号
                    .Borders.Weight = s3.Weight '边框线宽度(粗细)
                    .FormatConditions.Delete '删除条件格式
                End With
            End If
        End If
    Next
    Application.ScreenUpdating = False
End Sub   
(备注:本过程参考修订了:http://www.vbeach.net/bbs/archiver/?tid-6471.html处的代码)
效果如下图演示:

  本帖的程序测试在Excel2003下通过。引用的帮助内容出自Excel2003VBA中。希望本帖起到资料或者工具的作用,既方便自己的快速查阅,也方便了有此需求的朋友。

 [em05][em10][em10][em10]


作者: Aeolian-Vox    时间: 2006-11-30 19:30

发这个贴真是费老鼻子劲了,不是断线就是发生发帖错误,再不然就是所帖子字数超过.......。痛苦的1个小时。

(, 下载次数: 1654)


[em10][em10][em10][em10][em10][em10][em24][em24]
作者: SpeedRat    时间: 2006-12-1 01:44
谢谢你的辛苦劳动。[em23]
作者: cyberazor    时间: 2007-3-5 10:23
留个记号,好好学习了
作者: -南风-    时间: 2007-11-21 23:46
慢慢看,我要仔细地看。辛苦了[em17]
作者: zz5151353    时间: 2007-11-21 23:48

對新入門者相當有用
謝謝樓主提供分享 !!!

 

[em23][em23][em23]

 

[em24][em24][em24]

 

[em27][em27][em27]
作者: laosanjie    时间: 2007-11-22 13:42
下载收藏学习了。谢谢楼主!!!
作者: hxyz25    时间: 2008-4-1 21:47
真是山东及时雨,正需要这样的东西。顶顶顶!
作者: yagi2008    时间: 2009-2-7 16:29
佩服佩服!!!研究得非常透彻!!!
作者: tw0503    时间: 2009-5-19 13:09
标题: 学到很多
学到很多,谢谢
作者: hlopr    时间: 2009-6-9 11:47
谢谢楼主,收藏学习!
作者: smshijiea    时间: 2009-8-4 11:16
支持啊,非常感谢,好好学学
作者: 飞云楼主    时间: 2009-8-4 19:58
好东东,收下学习。向楼主致敬。
作者: dllaozeng    时间: 2009-8-12 15:29
辛苦了,正好用得着!!!
作者: single_star    时间: 2009-8-13 15:21
呵呵,想不到我跟楼主写的代码思路几乎一模一样(以下代码新增边框格式转换)
(樓主备注:本过程参考修订了:http://www.vbeach.net/bbs/archiver/?tid-6471.html处的代码,不知是否我以前在该处写的代码,我当时的ID名是stardust,不过该网站现在关闭了)


Sub Translate_Formatconditions_Celltype()
Set range0 = Application.Intersect(Selection, ActiveCell.SpecialCells(xlCellTypeAllFormatConditions))
If range0 Is Nothing Then Exit Sub
'*************************************************
Application.ScreenUpdating = False
Dim jd(8)      'set this for 8 FormatConditionOperator
jd(1) = "=And(cell0>=for1,cell0<=for2)"    'Between
jd(2) = "=Not(And(cell0>=for1,cell0<=for2))"       'notBetween
jd(3) = "=cell0=for1"               '=
jd(4) = "=cell0<>for1"              '<>
jd(5) = "=cell0>for1"               '>
jd(6) = "=cell0<for1"               '<
jd(7) = "=cell0>=for1"              '>=
jd(8) = "=cell0<=for1"              '<=

For Each sel0 In range0
n = sel0.FormatConditions.Count
  con = 0
  For i = n To 1 Step -1
  With sel0
  sel0.Select 'use this to avoid formula wrong answer with row() or column()
'****.FormatConditions(i).Type: xlCellValue
  If .FormatConditions(i).Type = 1 Then
    s0 = sel0.Value2
    operator0 = .FormatConditions(i).Operator
    a0 = Application.Evaluate(.FormatConditions(i).Formula1)
    If operator0 = 1 Or operator0 = 2 Then
    b0 = Application.Evaluate(.FormatConditions(i).Formula2)
    If Not (IsNumeric(s0)) Or Not (IsNumeric(a0)) Or Not (IsNumeric(b0)) Then
      If IsEmpty(s0) Then s0 = ""
      If IsNumeric(s0) Then s0 = CStr(s0)
      If IsEmpty(a0) Then a0 = ""
      If IsNumeric(a0) Then a0 = CStr(a0)
      If IsEmpty(b0) Then b0 = ""
      If IsNumeric(b0) Then b0 = CStr(b0)
      Else
      If IsEmpty(s0) Then s0 = 0
      If IsEmpty(a0) Then a0 = 0
      If IsEmpty(b0) Then b0 = 0
    End If

      If a0 > b0 Then
       a1 = b0
       b1 = a0
      Else
       a1 = a0
       b1 = b0
      End If
    Else
      a1 = a0
    End If
       If Application.WorksheetFunction.IsText(s0) Then s0 = """" & UCase(s0) & """"
      If Application.WorksheetFunction.IsText(a1) Then a1 = """" & UCase(a1) & """"
      If Application.WorksheetFunction.IsText(b1) Then b1 = """" & UCase(b1) & """"
      st0 = jd(operator0)
      st = Replace(st0, "for1", a1)
      st = Replace(st, "for2", b1)
      st = Replace(st, "cell0", s0)
      If Application.WorksheetFunction.IsError(Application.Evaluate(st)) Then
        ans = False
      Else
       ans = Application.Evaluate(st)
      End If
'****.FormatConditions(i).Type:xlExpression
    Else
      If Application.WorksheetFunction.IsError(Application.Evaluate(.FormatConditions(i).Formula1)) Then
        ans = False
      Else
       ans = Application.Evaluate(.FormatConditions(i).Formula1)
      End If
    End If
  End With
  If ans = True Then con = i
  Next
'***translate celltype(Font&Interior&Borders)
If con > 0 Then
  Set s1 = sel0.FormatConditions(con).Font
  Set s2 = sel0.FormatConditions(con).Interior
  With sel0.Font
    .Bold = s1.Bold
    .Italic = s1.Italic
    .Underline = s1.Underline
    .Strikethrough = s1.Strikethrough
    .ColorIndex = s1.ColorIndex
  End With
  sel0.Interior.Pattern = s2.Pattern
  sel0.Interior.ColorIndex = s2.ColorIndex
   
  For b0 = 1 To 4
   If sel0.FormatConditions(con).Borders(b0).LineStyle <> xlNone Then
      sel0.Borders(b0).LineStyle = sel0.FormatConditions(con).Borders(b0).LineStyle
      sel0.Borders(b0).Weight = sel0.FormatConditions(con).Borders(b0).Weight
      sel0.Borders(b0).ColorIndex = sel0.FormatConditions(con).Borders(b0).ColorIndex
   End If
  Next
End If

sel0.FormatConditions.Delete
Next
Application.ScreenUpdating = True
End Sub

[ 本帖最后由 single_star 于 2010-1-4 08:45 编辑 ]
作者: 老伙计2008    时间: 2009-9-5 10:17
好东东,收下学习。向楼主致敬。收藏!
作者: lzyamo3057    时间: 2009-9-5 10:20
学习了,谢谢楼主
作者: jxhm0520    时间: 2009-9-6 17:53
谢谢分享!
作者: lzqlaj    时间: 2010-1-1 17:00
好贴子,终于弄明白了边框的意义。
作者: 鱼求雨    时间: 2010-1-3 23:11
挺不劣,辛苦辛苦,感谢
作者: wwjjjj    时间: 2010-1-20 09:30
楼主太强大了,慢慢学习
作者: txbing    时间: 2010-3-13 14:12
佩服佩服!!!
作者: caowangxin    时间: 2010-4-9 22:26
感谢感谢!
作者: dybg    时间: 2010-4-12 01:25
下载收藏学习了。谢谢楼主!!!
作者: lilyforid    时间: 2010-5-13 09:04
辛苦了,谢谢,学习了
作者: lbw500    时间: 2010-6-4 22:09
标题: 请各位高手帮忙!
(, 下载次数: 44)
作者: shengjiri    时间: 2010-9-20 11:14
好贴就要顶啊
作者: ljx63426    时间: 2010-9-21 12:58
提示: 该帖被管理员或版主屏蔽
作者: lisan    时间: 2011-2-2 18:38
谢谢分享!我是来学习的,留个记号。
作者: cpa911    时间: 2011-2-2 21:18
真是专家啊!好厉害啊!佩服!先收藏了。
作者: extlocal    时间: 2011-2-7 00:22
好贴,留号学习!
作者: eitiwo    时间: 2011-2-19 10:05
我要仔细地看,楼主辛苦了。
作者: lhdcxz    时间: 2011-4-4 17:38
我如果只想得到“设置了条件格式的单元格格式条件是否为真”,如何用VBA代码表示呢?
作者: blackttea    时间: 2011-4-6 12:47
好东东,收下学习。向楼主致敬。收藏!
作者: jinsha2002    时间: 2011-4-6 16:26
好东东,收下学习。向楼主致敬
作者: lysm    时间: 2011-4-7 23:34
好贴,学之,顶之
作者: gjb_zjf    时间: 2011-4-8 12:36
谢谢分享!!!
作者: feifeizhuno1    时间: 2011-5-10 16:59
不是我毛病,实在是读完了楼主的帖子,没有很直观的找到怎么样判断一个条件格式是否成立,是有一个对应的属性还是需要iserror判断,没有说明。唉!
作者: yaojil    时间: 2011-5-10 17:42
先收下,慢慢学习,谢谢楼主
作者: mhts    时间: 2011-5-10 18:06
很不错,值得收藏,方便以后使用。
作者: Qinqinjiang    时间: 2011-6-27 21:21
谢谢你的辛苦劳动
作者: leiting18    时间: 2011-9-28 15:45
谢谢,不错,好好学习研究一下
作者: TasteTeaRoom    时间: 2011-11-17 11:34
這個真是不錯的說,找了好久了。得花時間好好學習下。
作者: bluexuemei    时间: 2011-12-22 21:46
留个脚印,慢慢学习!
作者: bluexuemei    时间: 2011-12-22 21:47
留个脚印,慢慢学习!
作者: 宇智波.波    时间: 2011-12-23 20:44
学习,学习

作者: mostin    时间: 2012-1-13 18:07
xlExpression 基于表达式的条件格式
记录一下
作者: lhdcxz    时间: 2012-1-14 12:19
好好学习。代码有解释,方便学习了。
作者: aptx48692112    时间: 2012-1-30 16:14
东西不错,
留个脚印,支持下!!
^_^
作者: xushaoming68    时间: 2012-2-17 18:31
谢谢楼主分享!
作者: ivw1979    时间: 2012-2-27 14:46
{:soso_e179:}
作者: sharkzhou    时间: 2012-2-27 22:09
学习中。。。。有用
作者: liucqa    时间: 2012-2-28 21:14
学习         
作者: 820211639    时间: 2012-3-9 12:19
厉害呀,什么都能!
作者: wspkiller    时间: 2012-5-7 14:43
强烈建议楼主编辑成为Word文档,谢谢
作者: zdqwy19    时间: 2012-9-8 08:05
在excel2007中Application.Evaluate(.FormatConditions(i).Formula1)的值总为true,不知何故。
作者: sunny_8848    时间: 2012-9-8 08:36
谢谢楼主的分享
作者: zdqwy19    时间: 2012-9-9 07:48
single_star 发表于 2009-8-13 15:21
呵呵,想不到我跟楼主写的代码思路几乎一模一样(以下代码新增边框格式转换)
(樓主备注:本过程参考修订了: ...

我把你的代码修改成提取单元格背景色的function进程,在excel2007中,当条件格式多于2个事,返回#value,当条件格式不多于2个时返回正常值。
作者: 赵莲    时间: 2012-9-12 00:48
{:soso_e179:}
作者: yb010    时间: 2013-2-2 16:57
不错!讲解详细。很久没用了。又来温习一遍。
作者: icepb    时间: 2013-4-18 09:59
以后要多看看MSDN了.
作者: yutiger0    时间: 2013-7-12 23:39
学习了,楼主辛苦!好多好长,慢慢看
作者: mutiantong    时间: 2013-8-26 13:02
学习,温故知新
作者: jinsha2002    时间: 2013-11-29 20:28
谢谢分享,做过标示,以后学习
作者: A0101    时间: 2014-3-1 12:17
学习中,楼主辛苦了
作者: hjqiang888    时间: 2014-5-13 23:26
很好的学习教程  谢谢
作者: VBA万岁    时间: 2014-5-19 09:03
Aeolian-Vox 发表于 2006-11-30 19:30
发这个贴真是费老鼻子劲了,不是断线就是发生发帖错误,再不然就是所帖子字数超过.......。痛苦的1个小时。 ...

收藏了,谢谢分享!

作者: lpclimber    时间: 2014-12-1 22:13
太好的整理资料了,谢谢楼主分享!!
作者: 970917716    时间: 2014-12-19 09:05
MARK。。。。。慢慢学习
作者: lmylmy224    时间: 2015-1-14 20:50
支持啊,非常感谢,好好学学
作者: 05050818    时间: 2016-11-4 13:44
谢谢楼主分享
作者: Anubarak    时间: 2017-1-3 14:19
太好了 很有用
作者: 冰山之剑    时间: 2017-5-28 04:17
学习了  收藏中,谢谢EH
作者: exaction    时间: 2017-8-3 15:42
收藏慢慢学习
作者: david_jiao    时间: 2018-9-9 13:00
谢谢楼主的无私分享,
下载收藏学习了。谢谢楼主!!!
作者: aman1516    时间: 2021-2-18 12:21
贴到用时方恨少,15年前的好贴,继续发扬知识的能量

作者: xiaomils    时间: 2021-2-18 12:22
很好,学习了
作者: 你叫小B超啊    时间: 2021-3-12 14:06
不错,学习了
作者: memao1    时间: 2021-12-29 14:22

谢谢楼主,收藏学习!
作者: frl-2005    时间: 2022-3-25 16:28
楼主辛苦,好人啊!
作者: 一条小笨笨    时间: 2022-7-22 23:05
留个名,好好学习下





欢迎光临 ExcelHome技术论坛 (https://club.excelhome.net/) Powered by Discuz! X3.4