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

--对插入的表格重命名

 

 

posted @ 2022-12-03 19:18  vba是最好的语言  阅读(203)  评论(0编辑  收藏  举报