针对选中为选中单元格操作
' 针对选中为选中单元格操作
Sub AddRowToSelectedTable()
Dim selectedTable As Cells
Dim selectedCell As cell
Dim lastRow As Object
Dim cellToMerge As Cells
Dim newCells As Range
Dim select_Cell As Cells
' MsgBox Selection.Type
' 检查是否有选中的单元格,4 为单元格,5 为表格
If Selection.Type = 4 Then
' 获取选中的单元格对象
Set selectedCell = Selection.Cells(1)
' 获取选中单元格所在的表格对象
Set selectedTable = selectedCell.Tables.Parent.Column.Cells
' Dim i As Long
' i = selectedTable.Count
' 获取表格的最后一行
Set lastRow = selectedTable(i)
' 在表格的最后一行添加一行
lastRow.Select
Selection.InsertRowsBelow
Set select_Cell = Selection.Cells
' 合并单元格
select_Cell.Merge
' 设置新行的列数为4
' With Selection.Tables(1)
' .Columns.Add
' .Columns.Add
' .Columns.Add
' End With
' 拆分单元格,NumRows为行数,NumColumns为列数
select_Cell.Split NumRows:=2, NumColumns:=4
' 提示添加成功
MsgBox "行已成功添加到选中的表格的最后一行,并且包含4列。"
Else
' 如果没有选中单元格,则提示错误
MsgBox "请先选中一个单元格。"
End If
End Sub
针对选中为选中表格操作
' 针对选中为选中表格操作
Sub AddRowsToSelectedTable()
Dim selectedTable As Table
' 显示5为表格
MsgBox Selection.Type
MsgBox wdSelectionTable
' 检查是否有选中的表格
If Selection.Type = 5 Then
' 获取选中的表格对象
Set selectedTable = Selection.Tables(1)
' 在选中的表格中添加一行
selectedTable.Rows.Add
' 提示添加成功
MsgBox "行已成功添加到选中的表格。"
Else
' 如果没有选中表格,则提示错误
MsgBox "请先选中一个表格。"
End If
End Sub