只要标题出现在工作表上,VBA列就不会删除

时间:2018-07-12 18:40:53

标签: excel vba

我将一张纸粘贴并粘贴到另一张纸上,但是数据量总是在变化。每当我粘贴时,工作表末尾的标签为“总计”的列就会出现,但是它弄乱了另一工作表上的数据,因此我想删除它。它从第二个代码中删除,但不删除第一个。我需要帮助弄清楚为什么第一个不起作用。

 Set RidGrTotal = Range("B1:AH3000")
    isum.Sheets("pivot custom").Activate
    For Each cell In RidGrTotal
        If cell.Value = "Grand Total" Then cell.EntireColumn.ClearContents
    Next  

    Set RidGrandTotal = Range("B1:AH3000")
    isum.Sheets("Pivot to MW").Activate
    For Each cell In RidGrandTotal
        If cell.Value = "Grand Total" Then cell.EntireColumn.ClearContents
    Next

1 个答案:

答案 0 :(得分:1)

正确限定范围,然后can avoid relying on Activate and all the problems that arise from implicit qualifications, etc.

Dim cell as Range
Dim addr = "B1:AH3000"


For Each cell In isum.Sheets("pivot custom").Range(addr)
    If cell.Value = "Grand Total" Then cell.EntireColumn.ClearContents
Next  

For Each cell In isum.Sheets("Pivot to MW").Range(addr)
    If cell.Value = "Grand Total" Then cell.EntireColumn.ClearContents
Next