2017-04-19 73 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 =范围(“”)