VBA宏,当前选定的单元格和行中的下一个单元格

时间:2014-10-23 09:10:16

标签: excel vba excel-vba cell

我正在尝试编写一个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程序员,而不是一个优秀的人。在此先感谢您的任何帮助:)

1 个答案:

答案 0 :(得分:0)

假设您正在选择G列中的任何单元格,

Range("G17:J17")应该是activeCell.offset(0,1)

如果你不这样做,你可能想要调整一下 - 查找ActiveCell() property - 它基本上将当前激活的单元格作为Range()对象返回

然后公式可以是"=$K$" & activecell.row

相关问题