2017-04-19 186 views
0

我正在使用以下vba代碼來應用條件格式。Vba只應用條件格式的頂部/底部邊框?

Sub ResetConditions() 
    With Worksheets(1).Range("A9:P1048576") 
     .FormatConditions.Add Type:=xlExpression, Formula1:= _ 
      "=ROW(B9)=ROW(OFFSET($B$9,COUNTA($B:$B)-2,0))" 
     With .FormatConditions(.FormatConditions.Count) 
      .SetFirstPriority 

      With .Borders 
      .LineStyle = xlContinuous 
      .Weight = xlThin 
      .Color = vbRed 
      End With 

     End With 
    End With 
End Sub 

的邊框顯示爲這樣:

enter image description here

但我希望它看起來像這樣:

enter image description here

我想只設置頂部/底部像這樣的邊界:

Sub ResetConditions() 
     With Worksheets(1).Range("A9:P1048576") 
      .FormatConditions.Add Type:=xlExpression, Formula1:= _ 
       "=ROW(B9)=ROW(OFFSET($B$9,COUNTA($B:$B)-2,0))" 
      With .FormatConditions(.FormatConditions.Count) 
       .SetFirstPriority 

       With .Borders(xlEdgeTop) 
       .LineStyle = xlContinuous 
       .Weight = xlThin 
       .Color = vbRed 
       End With 

      End With 
     End With 
    End Sub 

但我不斷收到一個錯誤,無法設置邊框類的linestyle屬性。

請有人能告訴我我要去哪裏嗎?

+0

嘗試錄製宏,而使用頂部設置條件格式邊界格式。你會看到,'Excel'本身將使用'.Borders(xlTop)'而不是'.Borders(xlEdgeTop)'。所以可能[邊框對象](https://msdn.microsoft.com/en-us/library/office/ff837809.aspx)與[FormatCondition.Borders](https:// msdn。 microsoft.com/en-us/library/office/ff196030.aspx)以這種沒有記錄的方式。 –

回答

0

嘗試像這樣...

Sub ResetConditions() 
    Dim ws As Worksheet 
    Dim Rng As Range 
    Dim n As Integer 
    Set ws = Sheets(1) 
    Set Rng = ws.Range("A9:P1048576") 

    Rng.FormatConditions.Add Type:=xlExpression, Formula1:= _ 
     "=ROW(B9)=ROW(OFFSET($B$9,COUNTA($B:$B)-2,0))" 
    n = Rng.FormatConditions.Count 
    Rng.FormatConditions(n).SetFirstPriority 
    With Rng.FormatConditions(n).Borders(xlTop) 
     .LineStyle = xlContinuous 
     .Weight = xlThin 
     .Color = vbRed 
    End With 
    With Rng.FormatConditions(n).Borders(xlBottom) 
     .LineStyle = xlContinuous 
     .Weight = xlThin 
     .Color = vbRed 
    End With 
End Sub 
0

這是我用什麼樣的邊界範圍:

Public Sub BorderMe(my_range) 

    Dim l_counter As Long 

    For l_counter = 7 To 10 '7 to 10 are the magic numbers for xlEdgeLeft etc 
     With my_range.Borders(l_counter) 
      .LineStyle = xlContinuous 
      .Weight = xlMedium 
     End With 
    Next l_counter 

End Sub 

您可以編輯顏色,重量,款式等 神奇IST是7,8,9和10是Excel的數對於xlEdgeLeft,xlEdgeRight,xlEdgeTopxlEdgeBottom

像這樣運行:call borderme(selection)立即窗口,看看是什麼。

0
Rng.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ 
    Formula1:="=10" 
Rng.FormatConditions(Rng.FormatConditions.Count).SetFirstPriority 
With Rng.FormatConditions(Rng.FormatConditions.Count).Borders(xlTop) 
    .LineStyle = xlContinuous 
    .TintAndShade = 0 
    .Weight = xlThin 
    .Color = vbRed 
End With 
With Rng.FormatConditions(Rng.FormatConditions.Count).Borders(xlBottom) 
    .LineStyle = xlContinuous 
    .TintAndShade = 0 
    .Weight = xlThin 
    .Color = vbRed 
End With 

試試這個代碼不要忘記設置RNG設置RNG =範圍(「」)