Excel VBA - 仅在顶部和底部添加边框的总线
问题描述:
我想在总线的底部添加边框线和底部的边框线Excel VBA - 仅在顶部和底部添加边框的总线
例如,我有第2到第3行和第3-4列的数据,然后我在第5行添加了一条总和线2-3。总和线
我想在第5行的顶部和底部添加一条边界线,只能到第4列。
我可以使用变量LastRow + 2(注意在最后一行数据和总行之间有一个空行)和LastColumn如何在范围(“A5:D5”)中。选择,因为每次都会变化?
我当前的代码:
Range("A5:D5").Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
答
我觉得NexttRow的事情仍然是一个好主意,该代码可以简化回落,这会增加之和格式化从2行之行数据的底部,可以是任何地方:
NR = Range("A" & Rows.Count).End(xlUp).Row + 1
Range("C" & NR, "D" & NR).FormulaR1C1 = "=SUM(R2C:R[-1]C)"
With Range("A" & NR, "D" & NR)
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).Weight = xlThin
End With
答
你并不真的需要LASTROW或LASTCOL变量。只需参照你的范围的最后一行,如下所示:
With Range("A5:D5")
With .Rows(.Rows.Count)
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End With
End With
你可以把它推广到你传递一个范围的子例程。