简体   繁体   English

VBA删除表后的行

[英]VBA delete rows after table

I have a spreadsheet in excel 2010 with a macro that resizes a table (table 1) based upon the size of another table (table 2), as they should always have the same number of rows. 我在excel 2010中有一个电子表格,其中包含一个宏,该宏会根据另一个表(表2)的大小来调整表(表1)的大小,因为它们应始终具有相同的行数。 The number of rows in table 2 changes on a montly basis. 表2中的行数每月都会变化。 This macro works fine as long as table 2 is adding rows. 只要表2要添加行,此宏就可以正常工作。 If table 2 is subtracting rows, then I wind up with formulas / data under table 1 that is unneeded. 如果表2减去行,那么我不需要的表1下的公式/数据就结束了。 Macro I'm using: 我正在使用的宏:

Sub Table 1()
    Sheet3.Unprotect Password:="password"
        ThisWorkbook.Sheets("Sheet3").ListObjects("Table 1").Resize [range]
        Sheet3.Protect Password:="password"
    End Sub

Is there a way to write a Macro to remove this extraneous data (all data / formulas after the current table)? 有没有一种方法可以编写宏来删除此无关数据(当前表之后的所有数据/公式)? I know it's a simple matter to delete them manually, but I'm hoping to protect the tab containing table one, and only allow macros (that will also be protected) to run, to avoid random people messing with this worksheet. 我知道手动删除它们很简单,但是我希望保护包含表1的选项卡,并且只允许运行宏(也会受到保护),以避免随机的人弄乱此工作表。 Any suggestions are appreciated. 任何建议表示赞赏。

Thanks! 谢谢! Graham 格雷厄姆

Calculate the number of rows before resizing and then after resizing. 在调整大小之前,然后在调整大小之后,计算行数。 Then, delete the difference rows if your table got smaller. 然后,如果表变小,请删除差异行。 In my example, my table is named Table1 , so you need to make a couple edits. 在我的示例中,我的表名为Table1 ,因此您需要进行一些编辑。 Give the following a try. 请尝试以下方法。

简短的调整表大小问题的CRUTER示例

Sub test()

Dim rowsBefore As Integer
Dim rowsAfter As Integer
Dim rowsExtra As Integer

'get num of rows before resize
rowsBefore = Range("Table1").Rows.Count

'resize table
ThisWorkbook.Sheets("Sheet3").ListObjects("Table 1").Resize [range]

'get num of rows after resize
rowsAfter = Range("Table1").Rows.Count

'get num of rows removed / added
rowsExtra = rowsBefore - rowsAfter

'remove data if positive / removed rows
If rowsExtra > 0 Then
    Range("Table1[testColumn]")(rowsAfter + 1).Select
    Range(ActiveCell.Address, ActiveCell.Offset(1 + rowsExtra).Address).EntireRow.Delete
End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM