【发布时间】:2015-02-10 05:57:26
【问题描述】:
我正在尝试编写一个 VBA 宏来应用执行以下操作的条件格式:
如果当前选中的单元格不等于右边的单元格,则更改填充颜色。到目前为止,我有这个:
Sub Macro8()
'
' Macro8 Macro
'
' Keyboard Shortcut: Ctrl+e
'
Cells.FormatConditions.Delete
Range("G17:J17").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
Formula1:="=$K$17"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = True
End Sub
问题在于范围("G17:J17"),这是一个合并的单元格,以及公式("$K$17")。
应该是Range("G$CurrentRow:J$CurrentRow") 和Formula1:="=$K$CurrentRow",但我就是不知道语法。
抱歉,我提出了一个愚蠢的问题,但我是一名 PLC 程序员,而不是一名擅长编程的人。提前感谢您的帮助:)
【问题讨论】:
-
Range("G17:J17")可能应该是activeCell.offset(0,1)假设您选择G列中的任何单元格。如果你不这样做,那么你可能需要调整它 - 查找 ActiveCell() property -
谢谢,这似乎有效。现在让我困惑的只是公式
-
试试
"=$K$" & activecell.row -
我可以吻你,谢谢。