在范围内的每个单元格周围的边界

我正在试图创build一个简单的函数,将在一定范围内的每个单元格周围添加边框。 使用精彩的录音会产生很多无用的代码。 下面的代码将显示一个“表”的数据,在这个范围的每个单元格周围,我想添加一个边框。 在线我一直无法find一个简单或明确的答案。

所有的帮助非常感谢!

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 

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

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 

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

 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 

例子:

 Call SetRangeBorder(Range("C11")) Call SetRangeBorder(Range("A" & result)) Call SetRangeBorder(DT.Cells(I, 6)) Call SetRangeBorder(Range("A3:I" & endRow)) 

这是另一种方式

 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 

当我最初寻找这个问题时没有find这个页面,但这是我的最终结果。 诚实地抬起头,张贴在超级用户,但不知道是否属于那里或这里。

示例呼叫

 Call BoxIt(Range("A1:z25")) 

子程序

 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 

为了添加边界,试试这个,例如:

 Range("C11").Borders(xlEdgeRight).LineStyle = xlContinuous Range("A15:D15").Borders(xlEdgeBottom).LineStyle = xlContinuous 

希望语法是正确的,因为我已经在C#中完成了这个工作。

 xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid xlWorkSheet.Cells(1, 1).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlDataBarBorderType.xlDataBarBorderSolid