1. 程式人生 > 其它 >vba-對插入的表格進行操作

vba-對插入的表格進行操作

Sub 新增行(ByVal tabName As String)

    On Error Resume Next
    Set tb = ActiveSheet.ListObjects(tabName)
    
    If tb.ListRows.Count > 0 Then
        tb.ListRows.Add (tb.ListRows.Count + 1)
    Else
        tb.ListRows.Add (1)
    End If
    
End Sub


Sub 插入行(ByVal tabName As String, ByVal beginRow As Integer)

    Set tb 
= ActiveSheet.ListObjects(tabName) Debug.Print tb.ListRows.Count '立即視窗顯示 If tb.ListRows.Count = 0 Then tb.ListRows.Add (1) Else If (Selection.Row > tb.ListRows.Count + beginRow Or Selection.Row <= beginRow) Then MsgBox "請選中表格所在的行" Else tb.ListRows.Add (Selection.Row
- beginRow) End If End If End Sub Sub 刪除選中的行(ByVal tabName As String, ByVal beginRow As Integer) On Error Resume Next Set tb = ActiveSheet.ListObjects(tabName) If tb.ListRows.Count = 1 Then tb.ListRows.Add (1) tb.ListRows(2).Delete Else If (Selection.Row
> tb.Range.Rows.Count + beginRow - 2 Or Selection.Row <= beginRow) Then MsgBox "請選中表格所在的行" Else tb.ListRows(Selection.Row - beginRow).Delete End If End If End Sub Sub 刪除所有行(ByVal tabName As String) Set tbl = ActiveSheet.ListObjects(tabName) If tbl.ListRows.Count > 1 Then 'Delete all table rows except first row With tbl.DataBodyRange If .Rows.Count > 1 Then .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete Else End If End With Else If tbl.ListRows.Count = 1 Then tbl.ListRows(1).Delete End If End If If tbl.ListRows.Count = 1 Then tbl.ListRows(1).Delete End If End Sub Sub 列印() ActiveWindow.SelectedSheets.PrintOut Copies:=1 End Sub Sub 刪除單(ByVal sheetname As String, ByVal tabName As String, ByVal orderNo As String) Set tb = Sheets(sheetname).ListObjects(tabName) If tb.ListRows.Count = 1 Then If tb.DataBodyRange(1, 2).Value = orderNo Then tb.ListRows(1).Delete End If Else x = 1 Do While x <= tb.ListRows.Count If tb.DataBodyRange(x, 2).Value = orderNo Then tb.ListRows(x).Delete Else x = x + 1 End If Loop End If If tb.ListRows.Count = 1 Then If tb.DataBodyRange(1, 2).Value = orderNo Then tb.ListRows(1).Delete End If End If End Sub Sub 儲存() ActiveWorkbook.Save End Sub

--對插入的表格重新命名