简体   繁体   English

清除宏可清除特定列中空白单元格上特定范围的单元格

[英]Clear Macro to clear specific range of cells on Blank cell in certain column

I asked a similar question yesterday to populate a range of cells within a specific list of columns. 昨天我问了一个类似的问题,以填充特定列列表中的一系列单元格。 That aspect of my code is working perfectly, but now I am trying to create a clear macro to delete the inserted rows. 我的代码的这一方面运行良好,但是现在我试图创建一个清除宏以删除插入的行。

All of the newly inserted rows will have blank cells in column N, so I am simply trying to search all cells in column N, find the blanks, then delete the row between columns N and Y. This is what I have so far, but it is not doing anything when I run it? 所有新插入的行在N列中都将有空白单元格,因此我只是尝试搜索N列中的所有单元格,找到空白,然后删除N列和Y列之间的行。运行时它什么也没做?

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Sub Clear_PCOTCS()
    Dim myLastRow As Long
    Dim myRow As Long

    Application.ScreenUpdating = False
    myLastRow = Cells(Rows.Count, "N").End(xlUp).Row
    For myRow = myLastRow To 1 Step -1
        If Len(Cells(myRow, "N")) = 1 And Cells(myRow, "N") = "" Then
            Range(Cells(myRow + 1, "N"), Cells(myRow + 1, "Y")).Delete  Shift:=xlDown 'CopyOrigin:=xlFormatFromLeftOrAbove
        End If
    Next myRow
    Application.ScreenUpdating = True
End Sub

Your error is coming from passing a bad enum to Range.Delete . 您的错误来自将错误的枚举传递给Range.Delete It expects an XlDeleteShiftDirection value, and you're passing it an XlDirection . 它需要一个XlDeleteShiftDirection值,并且您要为其传递XlDirection Note that although xlShiftUp and xlUp share the same value, xlDown (-4121) is not a valid XlDeleteShiftDirection . 请注意,尽管xlShiftUpxlUp共享相同的值,但xlDown (-4121)不是有效的XlDeleteShiftDirection

If all you need to do is delete every row (between columns "N" and "Y") when the cell in column "N" is empty, you're over-thinking it. 如果您需要做的就是在“ N”列中的单元格为空时删除每一行(在“ N”列与“ Y”列之间),则说明您的想法太多了。 First, the shift should default to xlShiftUp based on the shape of the Range that you're creating to delete, so you can simply omit the parameter. 首先,根据您要删除的Range的形状,平移应默认为xlShiftUp ,因此您可以简单地省略该参数。 Second, you need to remove the offset you're applying here: 其次,您需要删除在此处应用的偏移量:

Range(Cells(myRow + 1, "N"), Cells(myRow + 1, "Y")).Delete

Delete the row that you're testing, not the one below it. 删除要测试的行,而不是其下面的行。 You should be looking for something more like this: 您应该在寻找更多类似这样的东西:

Sub Clear_PCOTCS()
    Dim myLastRow As Long
    Dim myRow As Long

    Application.ScreenUpdating = False
    myLastRow = Cells(Rows.Count, 14).End(xlUp).Row
    For myRow = myLastRow To 1 Step -1
        If Cells(myRow, 14).Value = vbNullString Then
            Range(Cells(myRow, 14), Cells(myRow, 25)).Delete
        End If
    Next myRow
    Application.ScreenUpdating = True
End Sub

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

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