边界内每个单元格周围的边框

Cus*_*omX 38 excel vba excel-vba

我正在尝试创建一个简单的函数,它将在一定范围内的每个单元格周围添加边框.使用精彩的录音会产生大量无用的代码.下面的代码将显示数据的"表格",在此范围内的每个单元格周围我想添加边框.在线我无法找到一个简单或明确的答案.

非常感谢所有帮助!

Set DT = Sheets("DATA")
endRow = DT.Range("F" & Rows.Count).End(xlUp).Row
result = 3

For I = 2 To endRow
    If DT.Cells(I, 6).Value = Range("B1").Value Then
        Range("A" & result) = DT.Cells(I, 6).Value
        Range("B" & result) = DT.Cells(I, 1).Value
        Range("C" & result) = DT.Cells(I, 24).Value
        Range("D" & result) = DT.Cells(I, 37).Value
        Range("E" & result) = DT.Cells(I, 3).Value
        Range("F" & result) = DT.Cells(I, 15).Value
        Range("G" & result) = DT.Cells(I, 12).Value
        Range("H" & result) = DT.Cells(I, 40).Value
        Range("I" & result) = DT.Cells(I, 23).Value
        result = result + 1
    End If
Next I
Run Code Online (Sandbox Code Playgroud)

Jon*_*ell 113

您只需要一行代码就可以设置范围内每个单元格的边框:

Range("A1:F20").Borders.LineStyle = xlContinuous

将多个效果应用于每个单元格周围的边框也很容易.

例如:

Sub RedOutlineCells()
    Dim rng As Range

    Set rng = Range("A1:F20")

    With rng.Borders
        .LineStyle = xlContinuous
        .Color = vbRed
        .Weight = xlThin
    End With
End Sub
Run Code Online (Sandbox Code Playgroud)


Oll*_*ren 12

可以使用任何范围作为参数调用以下内容:

Option Explicit

Sub SetRangeBorder(poRng As Range)
    If Not poRng Is Nothing Then
        poRng.Borders(xlDiagonalDown).LineStyle = xlNone
        poRng.Borders(xlDiagonalUp).LineStyle = xlNone
        poRng.Borders(xlEdgeLeft).LineStyle = xlContinuous
        poRng.Borders(xlEdgeTop).LineStyle = xlContinuous
        poRng.Borders(xlEdgeBottom).LineStyle = xlContinuous
        poRng.Borders(xlEdgeRight).LineStyle = xlContinuous
        poRng.Borders(xlInsideVertical).LineStyle = xlContinuous
        poRng.Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

例子:

Call SetRangeBorder(Range("C11"))
Call SetRangeBorder(Range("A" & result))
Call SetRangeBorder(DT.Cells(I, 6))
Call SetRangeBorder(Range("A3:I" & endRow))
Run Code Online (Sandbox Code Playgroud)


Cra*_*zyd 7

我有一组15个子程序,我添加到我创建的每个Coded Excel工作簿中,这是其中之一.以下例程清除区域并创建边框.

样本电话:

Call BoxIt(Range("A1:z25"))
Run Code Online (Sandbox Code Playgroud)

子程序:

Sub BoxIt(aRng As Range)
On Error Resume Next

    With aRng

        'Clear existing
        .Borders.LineStyle = xlNone

        'Apply new borders
        .BorderAround xlContinuous, xlThick, 0
        With .Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlMedium
        End With
        With .Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .Weight = xlMedium
        End With
    End With

End Sub
Run Code Online (Sandbox Code Playgroud)


Dic*_*ika 5

这是另一种方式

Sub testborder()

    Dim rRng As Range

    Set rRng = Sheet1.Range("B2:D5")

    'Clear existing
    rRng.Borders.LineStyle = xlNone

    'Apply new borders
    rRng.BorderAround xlContinuous
    rRng.Borders(xlInsideHorizontal).LineStyle = xlContinuous
    rRng.Borders(xlInsideVertical).LineStyle = xlContinuous

End Sub
Run Code Online (Sandbox Code Playgroud)