2012-09-07 63 views
0

我用下面的代碼在Excel細胞插入條件格式..如何有條件地格式化Excel VBA宏?

range("d" & rowno).Select 
Selection.Offset(1, 0).EntireRow.Insert 
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="= RC > 7" 
Selection.FormatConditions(Selection.FormatConditions.count).SetFirstPriority 
With Selection.FormatConditions(1).Interior 
    .PatternColorIndex = xlAutomatic 
    .Color = 65535 'Yellow 
    .TintAndShade = 0 
End With 

上面通過比較所定義比值磨碎這是「7」工作正常... 但是,如果我通過變量「lhigh」存儲值,同樣的im將它傳遞給formaula它不起作用。 例如; lhigh=7

range("d" & rowno).Select 
Selection.Offset(1, 0).EntireRow.Insert 
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="= RC > lhigh" 
Selection.FormatConditions(Selection.FormatConditions.count).SetFirstPriority 
With Selection.FormatConditions(1).Interior 
    .PatternColorIndex = xlAutomatic 
    .Color = 65535 'Yellow 
    .TintAndShade = 0 
End With 

請讓我知道我們如何能夠比計算大於檢查,如果我們通過變量,而不是直接的整數值

回答

2

你需要這樣的:

Formula1:="= RC > " & lhigh 

即你需要做的使用&運算符的字符串連接。然後將評估爲"= RC > 7"

+0

謝謝..它的工作原理 – user930679

2

如果我在Excel 2007

Sub test() 

Dim foo As Range 

Set foo = ActiveWorkbook.Sheets("Sheet1").Range("C3") 

With foo.FormatConditions _ 
     .Add(xlExpression, Formula1:="=Arc>lhigh") 

    With .Font 
     .Bold = True 
     .ColorIndex = 4 
    End With 
End With 

End Sub 

命名一個細胞「弧」,另一個是「lhigh」,那麼以下子的作品對我來說這將設置在小區C3條件格式,這將踢值時在Arc> lhigh中。

也許你應該簡化你的代碼到基本的東西,然後增加額外的複雜性。我猜你的問題在於你的代碼的另一部分。