通过VBA代码

时间:2016-02-15 19:19:04

标签: excel vba excel-vba

我无法在不同的工作表上将范围和单元格设置为空白值,除非工作表可见。

代码如下:

Sub Clean_Up_Ranges()
    Dim nm As Worksheet
        On Error Resume Next
    For Each nm In Worksheets
            Worksheets("nm").Select
         Range("d7:d37") = ""
        Range("k7:k37") = ""

        Range("L40:l42") = ""
'..... plenty more ranges
        Range("h80:j179") = ""
        Calculate

        Debug.Print nm.Name

    Next
    On Error GoTo 0

End Sub

任何想法都赞赏。

2 个答案:

答案 0 :(得分:1)

明确参考表格。

Sub Clean_Up_Ranges()
    Dim nm As Worksheet
        On Error Resume Next
    For Each nm In Worksheets
            'Worksheets("nm").Select
         nm.Range("d7:d37") = ""
        nm.Range("k7:k37") = ""

        nm.Range("L40:l42") = ""
'..... plenty more ranges
        nm.Range("h80:j179") = ""
        Calculate

        Debug.Print nm.Name

    Next
    On Error GoTo 0

End Sub

答案 1 :(得分:0)

您可以在代码中使用Activate关键字来激活工作表。

See This了解SHEETS.SELECT或SHEETS.ACTIVATE之间的区别。

Sub Clean_Up_Ranges()
    Dim nm As Worksheet
    On Error Resume Next

    For Each nm In Worksheets
         nm.Activate              'Activate the sheet

         Range("d7:d37") = ""
        Range("k7:k37") = ""

        Range("L40:l42") = ""
'..... plenty more ranges
        Range("h80:j179") = ""
        Calculate

        Debug.Print nm.Name

    Next
    On Error GoTo 0

End Sub