[英]Find last used cell in a specific column and border around with a range
只是動態輸入的一個示例(可以改進):
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim rng1 As Range, rng2 As Range
Dim lc As Long
If Not Intersect(Range("3:3"), Target) Is Nothing Then
lc = Range("K3").CurrentRegion.Columns.Count 'No xlToLeft just in case far to the right there might be other cells
Set rng1 = Range(Cells(3, 11), Cells(10, 11 + lc))
Set rng2 = Range(Cells(3, 11), Cells(10, 11 + lc - 1))
rng1.Borders.LineStyle = xlNone
rng2.BorderAround ColorIndex:=1
End If
Application.EnableEvents = True
End Sub
到目前為止,這僅在每列添加/刪除時有效...如上所述,可以在;)上進行改進
應該這樣做:
Sub Sample()
Dim rng As Range
Dim LastCol As Long
With ThisWorkbook.Sheets("MySheet") 'Change MySheet for your sheet name
LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column 'this will find the last used column on Row 3
Set rng = Range("K3", .Cells(10, LastCol))
End With
With rng
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlThin
End With
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.