2012-03-16 96 views
5

我想在總線的底部添加邊框線和底部的邊框線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 

回答

3

我覺得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 
2

你並不真的需要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 

你可以把它推廣到你傳遞一個範圍的子例程。