在特定列中查找最后使用的单元格并用范围包围
Find last used cell in a specific column and border around with a range
我有这个 Excel,值从 K3 开始(用户将他们的值从那个单元格开始,所以我不知道会有多长Row 3) 我想从 K3 到该行的最后一个值(Row3)从 Row10 到 Row3 中使用的最后一个值(对不起,我真的不知道怎么说,我会举一个例子让你更好的理解)
这是一张图片
我找到的唯一代码是这个,但它只适用于静态范围。
Sub Sample()
Dim rng As Range
Set rng = Range("K3:P10")
With rng.Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
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
只是一个动态输入的例子(可以改进):
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
到目前为止,这仅在每列 adding/deleting 时有效...如前所述,它可以改进 ;)
我有这个 Excel,值从 K3 开始(用户将他们的值从那个单元格开始,所以我不知道会有多长Row 3) 我想从 K3 到该行的最后一个值(Row3)从 Row10 到 Row3 中使用的最后一个值(对不起,我真的不知道怎么说,我会举一个例子让你更好的理解)
这是一张图片
我找到的唯一代码是这个,但它只适用于静态范围。
Sub Sample()
Dim rng As Range
Set rng = Range("K3:P10")
With rng.Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
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
只是一个动态输入的例子(可以改进):
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
到目前为止,这仅在每列 adding/deleting 时有效...如前所述,它可以改进 ;)