使用包含条件格式说明的录制宏时出现问题

时间:2017-01-06 20:17:06

标签: excel vba excel-vba macros

我在网站上搜索了这个问题,虽然有人质疑这个问题但回复并不令我满意,因为OP报告的问题没有得到直接解决。相反,提供了避免问题的解决方法,而不是解释为什么代码不起作用。

所以我在这里尝试我的版本。

我的电子表格中已经有一些条件格式。具体地,有两种条件格式分别应用于列U和V.现在我希望将新的条件格式应用于整个相关数据范围,这包括U和V列中的内容。

我录制了以下宏,在我录制它的时候工作正常。但是,单独运行宏会产生错误。这是有问题的宏:

Sub Macro13()
'
' Macro13 Macro
'

'
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$C1<>$C2"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Borders(xlBottom)
        .LineStyle = xlContinuous
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

宏是逐字的,也就是说,它就像Excel生成它一样。

错误“无法设置Border类的LineStyle属性”出现在.LineStyle = xlContinuous行中。

任何人都可以帮我理解为什么当我录制它时它会起作用但是当我运行它时却不行?如何更改它以使其正常工作?

我在Windows 7 Professional计算机上使用Excel 2007。

更新 如果我在代码中插入“Selection.FormatConditions.Delete”行,如下所示:

Sub Macro13()
'
' Macro13 Macro
'

'
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select

    Selection.FormatConditions.Delete  <-----------Added here

    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$C1<>$C2"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Borders(xlBottom)
       .LineStyle = xlContinuous
       .TintAndShade = 0
        .Weight = xlThin
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

然后代码正常运行并产生我想要的下划线,但它也删除了列U和V的先前条件格式,使得更改无用。

1 个答案:

答案 0 :(得分:0)

请参阅下面的注释代码以获取解释。另请参阅this link,了解如何避免使用Select - 宏录制器因臭名昭着的原因!

Sub AddFormatting()

    ' Create a range object so Select isn't needed, use the same xlToRight and xlDown methods though

    Dim myRange As Range

    With ActiveSheet
        Set myRange = .Range(.Range("A1"), .Range("A1").End(xlToRight).End(xlDown))
    End With

    ' Note you may not want to use the above method, as it formats the whole document if there is nothing in row 1!!
    ' You could remove the above With block and consider using instead:
    ' Set myRange = ActivesSheet.UsedRange

    ' Add a new Format Condition
    myRange.FormatConditions.Add Type:=xlExpression, Formula1:="=$C1<>$C2"

    ' Make it the first Format Condition (Macro recorder's default)
    ' Note you may not want this priority order in your conditional formats!
    ' For now though, it makes referencing the format simpler as it's now FormatConditions(1)

    myRange.FormatConditions(myRange.FormatConditions.Count).SetFirstPriority

    With myRange.FormatConditions(1).Borders(xlBottom)
        .LineStyle = xlContinuous
        .TintAndShade = 0
        .Weight = xlThin
    End With

    ' Again, this is excel's default but may not be what you want, look up StopIfTrue
    myRange.FormatConditions(1).StopIfTrue = False

End Sub

最后一点:

您可能实际上想要在此子类的开头删除工作表中的所有先前条件格式,并在VBA中以相同的方式重新创建它们。这是因为与Excel中的单元格交互经常会分裂并使条件格式变得复杂 - 创建一个慢慢停止的文档!它还可以确保您不会重复添加上面实际创建的条件格式。

希望这有帮助。