我正在尝试创建一个简单的函数,它将在特定范围内的每个单元格周围添加边框。使用精彩的录音会产生大量无用的代码。下面的代码将显示数据的“表格”,在此范围内的每个单元格周围我想添加边框。在线我无法找到一个简单或明确的答案。
非常感谢所有帮助!
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
答案 0 :(得分:103)
您只需要一行代码就可以设置范围内每个单元格的边框:
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
答案 1 :(得分:10)
可以使用任何范围作为参数调用以下内容:
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))
答案 2 :(得分:6)
我有一组15个子程序,我添加到我创建的每个Coded Excel工作簿中,这是其中之一。以下例程清除区域并创建边框。
示例电话:
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
答案 3 :(得分: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
答案 4 :(得分:0)
要添加边框,请尝试此操作,例如:
Range("C11").Borders(xlEdgeRight).LineStyle = xlContinuous
Range("A15:D15").Borders(xlEdgeBottom).LineStyle = xlContinuous
希望语法正确,因为我已经在C#中完成了这个。
答案 5 :(得分:0)
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
答案 6 :(得分:0)
您还可以将此任务包含在另一个宏中,而无需打开新的宏:
我不放置Sub和end Sub,因为如下图所示,宏包含更长的代码
With Sheets("1_PL").Range("EF1631:JJ1897")
With .Borders
.LineStyle = xlContinuous
.Color = vbBlack
.Weight = xlThin
End With
[![enter image description here][1]][1]End With