简体   繁体   English

Excel VBA:如果特定工作表上的特定列为空白,则删除空白列和整个行

[英]Excel VBA: Delete blank columns & entire rows if specific columns are blank on specific worksheet not working

My problem is the macro won't work on a specified worksheet, only the active one. 我的问题是,宏将无法在指定的工作表上运行,只能在活动的工作表上运行。 I have two subroutines for deleting entire columns, and then deleting entire rows if specific columns are blank. 我有两个子例程,用于删除整个列,然后在特定列为空的情况下删除整个行。 I want to make it work for a specific worksheet, which I understood to be With Worksheets("OutPut") but it still culls the active worksheet. 我想使其适用于特定的工作表,据我With Worksheets("OutPut")With Worksheets("OutPut")但它仍会删除活动的工作表。

It works as intended so long as the active worksheet is selected. 只要选择了活动的工作表,它就可以正常工作。

Sub DeleteBlankColumns()
    With Worksheets("OutPut")
        Set MyRange = Worksheets("OutPut").UsedRange
        For iCounter = MyRange.Columns.Count To 1 Step -1
            If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
            Columns(iCounter).Delete
            End If
        Next iCounter
        End With
End Sub

And

Sub QuickCull()
    With Worksheets("OutPut")
        On Error Resume Next
        Columns("B").SpecialCells(xlBlanks).EntireRow.Delete
        On Error Resume Next
        Columns("C").SpecialCells(xlBlanks).EntireRow.Delete
        On Error Resume Next
        Columns("D").SpecialCells(xlBlanks).EntireRow.Delete
        On Error Resume Next
        Columns("E").SpecialCells(xlBlanks).EntireRow.Delete
        End With
End Sub

There's a button to Call both of them, which again, will work if the worksheet I want to transform is active. 有一个同时Call它们的按钮,如果要转换的工作表处于活动状态,它将再次起作用。 For reference, this is intended to be appended on an existing company macro, so simply running it on the worksheet when it's active won't work. 作为参考,它打算附加在现有的公司宏上,因此仅在工作表处于活动状态时在工作表上运行它是行不通的。

Thank you! 谢谢!

For the first code sample, 对于第一个代码示例,

If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
     Columns(iCounter).Delete

should be 应该

If Application.CountA(.Columns(iCounter).EntireColumn) = 0 Then
     .Columns(iCounter).Delete

(a "." before Columns, to specify the sheet) (在列之前的“。”,用于指定工作表)

Same thing for the second code sample 第二个代码示例也是如此

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

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