excel 是否有办法清除表格下方动态范围内的内容?

koaltpgm  于 2023-03-09  发布在  其他
关注(0)|答案(2)|浏览(176)

我在一个工作表上有3个表,现在我有vba用于在每次调整表1的大小时调整表2和表3的大小,但是在调整表2和表3的大小时是否可以清除表2和表3下面的所有内容,因为公式被保留下来了
下面是调整大小的代码:

Dim Tbl_2 As ListObject
Dim Tbl_1 As ListObject
Dim Tbl_3 As ListObject

Set Tbl_1 = Sheet1.ListObjects("TableQuery")
Set Tbl_2 = Sheet1.ListObjects("Table2")
Set Tbl_3 = Sheet1.ListObjects("Table3")

If Tbl_3.Range.Rows.Count <> Tbl_1.Range.Rows.Count Then
    Tbl_3.Resize Tbl_3.Range.Resize(Tbl_1.Range.Rows.Count)
End If

If Tbl_2.Range.Rows.Count <> Tbl_1.Range.Rows.Count Then
    Tbl_2.Resize Tbl_2.Range.Resize(Tbl_1.Range.Rows.Count)
End If

下面的图片显示了在调整表的大小后,理想情况下要删除的内容:

nkhmeac6

nkhmeac61#

我对你的代码做了一些调整,因为我不确定是否允许我同时修改两个表,或者你是否可以在两个表之间放置某些东西,所以我做了一个额外的sub来缩短它。这将确保它只影响它在调整大小之前的单元格(因为你只调整行的大小,列保持不变)

Sub clearRowsAfterResizing()
    Dim Tbl_2 As ListObject
    Dim Tbl_1 As ListObject
    Dim Tbl_3 As ListObject
    Dim ws As Worksheet
    
    Set ws = ActiveWorkbook.Worksheets("Test2")
    Set Tbl_1 = ws.ListObjects("TableQuery")
    Set Tbl_2 = ws.ListObjects("Table2")
    Set Tbl_3 = ws.ListObjects("Table3")
    
    changeSize Tbl_2, Tbl_1, ws
    changeSize Tbl_3, Tbl_1, ws

 End Sub
 
 Sub changeSize(tblAdjust As ListObject, tblChanged As ListObject, ws As Worksheet)
    Dim lRow As Long, dif As Long, sCol As Long, lCol As Long
    lRow = tblAdjust.Range.Rows(tblAdjust.Range.Rows.Count).Row
    dif = tblAdjust.Range.Rows.Count - tblChanged.Range.Rows.Count
    If tblAdjust.Range.Rows.Count <> tblChanged.Range.Rows.Count Then
        tblAdjust.Resize tblAdjust.Range.Resize(tblChanged.Range.Rows.Count)
        If dif > 0 Then
            sCol = tblAdjust.Range.Columns(1).Column
            lCol = tblAdjust.Range.Columns(tblAdjust.Range.Columns.Count).Column
            With ws
                .Range(.Cells(lRow - dif + 1, sCol), .Cells(lRow, lCol)).Clear
            End With
        End If
    End If
 End Sub

希望这对你有帮助,如果你有任何问题随时问:)

e4eetjau

e4eetjau2#

调整“从属”表的大小

Option Explicit

Sub ResizeSlaveTables()

    ' Define constants.
    Const MASTER_NAME As String = "TableQuery"
    Const SLAVE_NAMES As String = "Table2,Table3"
    
    ' Reference the worksheet (by code name). Great!
    Dim ws As Worksheet: Set ws = Sheet1
    
    ' Split the slave table names into an array.
    Dim Slaves() As String: Slaves = Split(SLAVE_NAMES, ",")

    ' Reference the master table.
    Dim mlo As ListObject: Set mlo = ws.ListObjects(MASTER_NAME)
    ' Calculate its number of rows.
    Dim mrCount As Long: mrCount = mlo.Range.Rows.Count
    
    Dim Slave, srCount As Long, WasResized As Boolean
    
    ' Loop through the slave table names.
    For Each Slave In Slaves
        ' Reference the range of the slave table.
        With ws.ListObjects(Slave).Range
            ' Calculate its number of rows.
            srCount = .Rows.Count
            ' Act per requirement.
            If mrCount <> srCount Then ' the sizes are different
                .ListObject.Resize .Resize(mrCount) ' resize the slave
                If mrCount < srCount Then ' slave size was greater
                    ' Clear 'remains' below the new size of the slave.
                    .Resize(srCount - mrCount).Offset(mrCount).Clear
                'Else ' master size was greater; do nothing
                End If
                WasResized = True ' for the message box
            'Else ' the sizes are the same; do nothing
            End If
        End With
    Next Slave
    
    If WasResized Then ' at least one was resized
        MsgBox "Slave tables resized.", vbInformation
    Else ' none were resized
        MsgBox "The slave tables were of the correct size.", vbExclamation
    End If
    
End Sub

相关问题