【问题标题】:Recorded macro for conditional formatting error: "Unable to set the LineStyle property of the Border class"为条件格式错误录制的宏:“无法设置边框类的 LineStyle 属性”
【发布时间】:2017-01-06 20:17:06
【问题描述】:

我有一个工作表,其中有两种条件格式分别应用于 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 列的条件格式,使更改无用。

【问题讨论】:

  • 尝试删除以. 开头的三行,因为这些是默认设置?
  • 我无法让它抛出错误。即使使用现有格式,它也适用于我。
  • 运行此代码时不会出现错误;但是,我确实有一个想法为什么它可能不起作用......当按下 F8(在 VBA 中)时,代码的每个部分都将单独执行。您能看到在可能重置“选择”方向的错误之前是否发生了某些事情吗?究竟出现了什么错误?
  • 您对相关工作表有任何工作表保护吗?要使 VBA 更改格式,必须解锁工作表。
  • @CJC,如果我删除这些说明,那么什么也不会发生。我可以删除最后两个,但不能删除 .LineStyle 一个。这才是我真正需要的。它在适当的单元格下划线

标签: excel vba


【解决方案1】:

请参阅下面的注释代码以获取解释。另请参阅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 中的单元格交互经常会分裂并使条件格式复杂化——创建一个慢慢停止的文档!它还可以确保您不会重复添加上面实际创建的条件格式。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-25
    • 2017-07-17
    • 2019-12-10
    • 2016-11-29
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多