我正在尝试选择表格的最后一行,并使用围绕整行的外边框直到最后一列。这是我的代码
Cells(Application.Rows.Count, .Columns.Count).End(xlUp).BorderAround Weight:=xlMedium
在我之前
Cells(Application.Rows.Count, 1).End(xlUp).BorderAround Weight:=xlMedium
我的第二行代码仅限于第一个单元格。我需要它在行外边界的所有单元格。我尝试了不同的东西,比如把它变成一个"范围"只得到一个错误。这些是我最接近的尝试。我没有得到错误,但它没有做我需要它做的事情。
谢谢,
答案 0 :(得分:1)
BordersAround
属性绘制边框Option Explicit
Sub BorderAroundBottom()
Dim WS As Worksheet
Dim rFirst As Range, rLast As Range, rTable As Range
'Need to know where table starts
Const ColHdr As String = "ColA"
Set WS = Worksheets("sheet2")
'Find first cell of the table
'Can hardcode this if known
With WS.Cells
Set rFirst = .Find(what:=ColHdr, after:=.Cells(.Rows.Count, 1), _
LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, _
searchdirection:=xlNext, MatchCase:=False)
If rFirst Is Nothing Then
MsgBox "First Column Header not found"
Exit Sub
End If
Set rLast = .Cells(.Rows.Count, rFirst.Column).End(xlUp)
Set rLast = .Cells(rLast.Row, .Columns.Count).End(xlToLeft)
Set rTable = .Range(rFirst, rLast)
End With
With rTable
.Borders.LineStyle = xlNone
.Rows(.Rows.Count).BorderAround LineStyle:=xlContinuous, Weight:=xlMedium
End With
End Sub
答案 1 :(得分:0)
问题是,在两次代码尝试中,您只选择一个单元格。因为您使用的是Cells
方法,所以您只选择一个单元格。您需要将Cells
与Range
对象结合使用才能获得多细胞区域。
假设您的数据在Sheet1的单元格A1中开始,这里是工作代码:
Sub DrawBorder()
Dim rngBottomRowStart As Range
Dim rngBottomRowEnd As Range
Dim rngDataUpperLeftCell As Range
Set rngDataUpperLeftCell = Sheet1.Range("A1")
With rngDataUpperLeftCell
Set rngBottomRowStart = Sheet1.Cells(.End(xlDown).Row, .Column)
Set rngBottomRowEnd = Sheet1.Cells(rngBottomRowStart.Row, .End(xlToRight).Column)
End With
Sheet1.Range(rngBottomRowStart, rngBottomRowEnd).BorderAround Weight:=xlMedium
End Sub