使用参考公式的条件格式

时间:2015-08-10 18:44:40

标签: excel-vba vba excel

我正在处理使用Excel按钮运行代码的代码。此代码的目的是插入格式化的行,以便技术人员可以根据需要填写数据值。通过这种方式,技术人员可以根据需要添加尽可能多的行。

对于技术人员正在执行的测试之一,如果值低于某个阈值,我希望该值为条件格式灰色。这个问题是因为我继续将单元格行插入excel我不能引用相同的位置。我放置了按钮来插入这些行,我使用按钮位置作为参考。有没有办法使用FormulaR1C1或其他方法来实现这一目标?

我的代码如下所示:

Private Sub CommandButton2_Click()

Dim V_Val As Variant ' Make the voltage value a variable
Dim rs As Integer ' The push button location
Dim cs As Integer

V_Val = InputBox("Voltage Above")                           ' Ask user input
rs = ActiveSheet.Shapes("CommandButton2").TopLeftCell.Row   ' Get push button location
cs = ActiveSheet.Shapes("CommandButton2").TopLeftCell.Column

Worksheets("Format3").Rows("7:7").Copy                      'Always grab from the same location
Worksheets("Meas_sum").Rows(rs).Insert Shift:=xlDown

ActiveSheet.Cells(rs, cs + 1).Select                    ' Place the voltage value in
Selection.NumberFormat = "@"
Selection.Value = V_Val

    ' Sig Pulse Grey out code
Range(Cells(rs, cs + 16), Cells(rs, cs + 29)).Select
    Selection.FormatConditions.Add Type:=xlExpression, _
        ActiveSheet.Cells(r2 + 2, c2 + i).FormulaR1C1 = "=R[-1]C/R[-2]C"
        'Formula1:="=G6*1.5>Q6" 'Make above work from refference value
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.499984740745262
    End With

0 个答案:

没有答案