2017-04-11 174 views
2

試圖經由VBA應用條件格式到電子表格,將有25K +行。沒有設置最後一列或最後一行,因此由於某種原因難以應用下面的代碼。當我檢查每行的條件格式時,它始終指向第3行。如果我把RC」 & LASTCOL +3 &‘= FALSE’它認識到這一點,例如細胞RC25:條件格式與xlR1C1式

Range(Cells(3, FoundCol), Cells(lastrowRecon, FoundCol)).Select 
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=R[]C" & lastCol + 3 & "=FALSE" 
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
    With Selection.FormatConditions(1).Interior 
     .PatternColorIndex = xlAutomatic 
     .Color = 255 
     .TintAndShade = 0 
    End With 

回答

1

當我檢查它一直參照第3行所有的時間......每一行格式化條件,如果我把RC」 & LASTCOL +3 &‘= FALSE’它recogni以電池RC25爲例

RC25是xlA1樣式的單元格引用。它是RC欄中的第25行。

你不能把一個xlR1C1公式爲條件格式規則當Application.ReferenceStyle是xlA1;相反,您不能將xlA1樣式公式放入當前運行xlR1C1公式樣式的系統中。但是,在兩者之間翻轉或使用Application.ConvertFormula爲您切換公式很容易。 .FormatConditions.Add方法沒有Formula1R1C1參數。

我覺得你xlR1C1公式將作爲"=NOT(RC" & (lastCol + 3) & ")"更好。

Sub wqewqwew() 
    Dim lastCol As Long, xlA1formula As String 
    lastCol = 22 
    With Selection 
     .FormatConditions.Delete 
     Application.ReferenceStyle = xlA1 
     'when Application.ReferenceStyle = xlA1 
     xlA1formula = Application.ConvertFormula("=NOT(RC" & (lastCol + 3) & ")", xlR1C1, xlA1, , .Cells(1)) 
     With .FormatConditions.Add(Type:=xlExpression, Formula1:=xlA1formula) 
      .Interior.Color = 255 
      .SetFirstPriority 
     End With 

     .FormatConditions.Delete 
     Application.ReferenceStyle = xlR1C1 
     'when Application.ReferenceStyle = xlR1C1 
     With .FormatConditions.Add(Type:=xlExpression, Formula1:="=NOT(RC" & (lastCol + 3) & ")") 
      .Interior.Color = 255 
      .SetFirstPriority 
     End With 

     'switch back 
     Application.ReferenceStyle = xlA1 
    End With 
End Sub 
+0

在R1C1風格RC25意味着欄Y與公式單元格行。所以如果你在A1中使用= RC25,這意味着xlA1風格,它相當於$ Y1。 – sktneer

+0

@sktneer - 只有當'Application.ReferenceStyle = xlR1C1'。如果'Application.ReferenceStyle = xlA1'然後'= RC25'表示471列,行25 – Jeeped

+0

呀絕對正確的。我只是在討論問題標題的參考。 – sktneer

0

LASTCOL尚未分配的值,所以它總是將是零

同樣與lastrowrecon和FoundCol

+0

嗨,是的,我已經在代碼進一步。條件公式選取正確的列。只是有問題的行。下面是我的代碼用於獲取LASTCOL:LASTCOL = ActiveSheet.Cells(2 Columns.Count).END(xlToLeft).COLUMN – Conor

+0

,值是? –

+0

lastCol = 25在這種情況下 – Conor