Excel 条件格式错误的录制宏:“0”;无法设置边框类“的LineStyle属性”;
我有一个工作表,其中有两种条件格式分别应用于U列和V列 我希望对整个相关数据范围应用一种新的条件格式,这包括U列和V列中的内容 我录制了以下内容,这在录制时起作用Excel 条件格式错误的录制宏:“0”;无法设置边框类“的LineStyle属性”;,excel,vba,Excel,Vba,我有一个工作表,其中有两种条件格式分别应用于U列和V列 我希望对整个相关数据范围应用一种新的条件格式,这包括U列和V列中的内容 我录制了以下内容,这在录制时起作用 Sub Macro13() ' ' Macro13 Macro ' ' Range("A1").Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlDown)).Select
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
为什么这在录制时有效,但在运行时无效?如何更改它以使其正常工作
我正在Windows7专业计算机上使用Excel2007
我搜索了这个网站,有人问了我一个问题。这个问题没有得到解决。相反,提供了避免问题的变通方法,而不是解释代码无法工作的原因
更新如果插入行
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
Sub-Macro13()
'
'宏13宏
'
'
范围(“A1”)。选择
范围(选择,选择。结束(xlToRight))。选择
范围(选择,选择。结束(xlDown))。选择
Selection.FormatConditions.Delete'有关说明,请参阅下面的注释代码。另请参见如何避免使用选择——这是宏录制器臭名昭著的特点
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
Sub AddFormatting()
'创建一个范围对象,这样就不需要选择,但使用相同的xlToRight和xlDown方法
将myRange变暗为Range
使用ActiveSheet
设置myRange=.Range(.Range(“A1”),.Range(“A1”).End(xlToRight).End(xlDown))
以
'注意,您可能不想使用上述方法,因为如果第1行中没有任何内容,它会格式化整个文档!!
你可以用块删除上面的内容,然后考虑使用:
'设置myRange=ActivesSheet.UsedRange
'添加新的格式条件
myRange.FormatConditions.Add类型:=Xexpression,公式1:=“=$C1$C2”
'将其设置为第一个格式条件(宏录制器的默认设置)
'注意,您可能不希望在条件格式中使用此优先级顺序!
'但是现在,它使引用格式变得更简单,因为它现在是FormatConditions(1)
myRange.FormatConditions(myRange.FormatConditions.Count).SetFirstPriority
使用myRange.FormatConditions(1).Borders(xlBottom)
.LineStyle=xlContinuous
.TintAndShade=0
.Weight=xlThin
以
'同样,这是excel的默认设置,但可能不是您想要的,请查找StopIfTrue
myRange.FormatConditions(1).StopIfTrue=False
端接头
最后一项说明:
实际上,您可能希望删除工作表中此子部分开头的所有以前的条件格式,并在VBA中以相同的方式重新创建它们。这是因为在Excel中与单元格的交互经常会分裂,并使条件格式复杂化——创建一个文档时会慢慢停止!它还可以确保您不会双重添加上面实际创建的条件格式
希望这有帮助。尝试删除以开头的三行,因为这是默认设置?我无法让它抛出错误。即使使用现有格式,它也适用于我;然而,我确实有一个想法,为什么它可能不起作用。。。按F8键(在VBA中)时,代码的每个部分都将单独执行。你能看到在错误发生之前是否发生了可能会重置“选择”方向的事情吗?显示的错误到底是什么?您对相关工作表有任何工作表保护吗?VBA若要更改格式,必须解锁工作表。@CJC,如果我删除了这些指令,则不会发生任何事情。我可以删除最后两个,但不能删除.LineStyle。这才是我真正需要的。它对适当的单元格进行下划线
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