Excel VBA-仅在顶部和底部添加边框的总行 [英] Excel VBA - total line to add border on top and bottom only
问题描述
我想在总线的顶部添加一条边界线,在总线的底部添加一条边界线
I want to add a border line on top and a border line on bottom of the total line
例如.我有第2到3行以及第3-4列的数据,然后添加了一条总行,将第5行的2-3行相加.
Eg. I have data from rows 2 to 3 and columns 3-4, I have then add a total line which sums line 2-3 in row 5.
我想在第5行的顶部和底部添加边界线,并且只在第4列上添加边界线.
I want to add a border line on top and bottom of row 5 and only upto column 4.
我可以使用变量LastRow + 2(请注意,在最后一行数据和总行所在的位置之间有一个空行)和LastColumn在Range("A5:D5")中的运行方式如何.选择,因为这将是可变的每次?
Can I use variables LastRow + 2 (note I have a blank line between the last row of data and where the total line is) and LastColumn some how in Range("A5:D5").Select as this will be varible each time?
我当前的代码:
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仍然是个好主意,代码也可以简化,这增加了总和并将总和行从row2格式化为数据的底部,无论可能在哪里:
I think the NexttRow thing is still a good idea, and the code can be simplified down as well, this adds the sum and formats the sum row from row2 to the bottom of the data, wherever that may be:
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
这篇关于Excel VBA-仅在顶部和底部添加边框的总行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!