更改表格内单个单元格的边框颜色

时间:2017-06-07 04:46:47

标签: vba excel-vba excel

我有一个命名表作为范围(列表对象),我需要更改一个单元格的边缘顶部边框颜色,但代码不起作用,我需要一些帮助。以下是代码的一部分:

With ws
    .Unprotect Password:="pAtRiCiA"

    For Each ctrl In Me.Controls
        If Left(ctrl.Name, 5) = "texto" Then
            If ultimafila - 1 <> 8 Then
                If ctrl.Name = "textoCausas" Then
                    If Not IsError(Application.Match(Me.textoCausas.Value, ws.ListObjects(ActiveSheet.Name).ListColumns(1).DataBodyRange, 0)) Then
                        .Cells(ultimafila, ctrl.TabIndex) = ""
                        .ListObjects(ActiveSheet.Name).ListColumns(1).DataBodyRange(ultimafila, 1).Borders(xlEdgeTop).LineStyle = xlContinuous
                        .ListObjects(ActiveSheet.Name).ListColumns(1).DataBodyRange(ultimafila, 1).Borders(xlEdgeTop).ColorIndex = 3
                        .ListObjects(ActiveSheet.Name).ListColumns(1).DataBodyRange(ultimafila, 1).Borders(xlEdgeTop).Weight = xlThick
                    Else
                        .Cells(ultimafila, ctrl.TabIndex) = ctrl.Value
                    End If
                Else
                    .Cells(ultimafila, ctrl.TabIndex) = ctrl.Value
                End If
            Else
                .Cells(ultimafila, ctrl.TabIndex) = ctrl.Value
            End If
        End If
    Next ctrl
    .Cells(ultimafila, 2) = txtControles.Value

    .Rows(ultimafila).AutoFit
    .Rows(ultimafila).RowHeight = .Cells(ultimafila, 1).Height + 12
    .Protect Password:="pAtRiCiA", DrawingObjects:=True, Contents:=True, Scenarios:=True
End With

1 个答案:

答案 0 :(得分:0)

使用此测试代码:

Sub Test()

    With ActiveSheet.ListObjects(1).ListColumns(1).DataBodyRange(6, 1).Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 3
        .Weight = xlThick
    End With

End Sub

我得到了这个结果:

enter image description here

所以,你的格式化代码对我来说很好 您是否可以将某些条件格式或其他格式应用于该表?