Excel VBA - 如何 select 整个 table 边框

Excel VBA - How to select the whole table of borders

我是 Excel VB 的新手。我希望你能帮助解决我的问题。

我有几个这样的table

table 可以由用户编辑,例如插入新行等。

我想突出显示并复制最新的 table(最后一行)并将其粘贴到下一个可用的 space,向下 3 行。 我设法做到了这一点,但我只能突出显示并复制带有数据的单元格。因此,如果 table 中间有一个空行,或者最后一行有边框,它就不会正确复制,如下所示:

我仍然需要复制边框内的所有内容,包括空行,但我缺乏这样做的技能和知识。希望大家帮忙。

以下是我的代码:

Sub CopyPaste()
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    FirstColumn = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
    LastColumn = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    BelowLastName = Cells.Find("", After:=Cells(LastRow, FirstColumn), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row


    Cells(BelowLastName, FirstColumn).Select 'This selects C5 (refer image)
    Selection.Offset(-1).Select 'This selects C4 (refer image)
    Range(Selection, Cells(LastRow, LastColumn)).Select 'This highlights whole table
    Selection.Copy

    Cells(LastRow, FirstColumn).Select
    ActiveCell.Offset(3).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
End Sub

试试这个

Sub x()

Dim rEnd As Range, rStart As Range, i As Long

Set rEnd = Range("C" & Rows.Count).End(xlUp)

Do While rEnd.Offset(i).Borders(xlEdgeBottom).LineStyle = xlContinuous
    i = i - 1
Loop

Set rStart = rEnd.Offset(i + 1)

Range(rStart, rEnd).Resize(, 8).Copy rEnd.Offset(3)

End Sub