跳过不带IF语句的工作表

时间:2018-11-22 10:59:13

标签: excel vba

我在这里有一些代码,但是我希望它跳过工作表名称AggregatedCollated ResultsTemplateEnd。我试图在其中添加If语句以跳过这些语句,但似乎不喜欢它。

Sub FillBlanks()
    Dim ws As Worksheet
    Dim rng1 As Range
    Dim rng2 As Range

    For Each ws In Worksheets
        Set rng2 = ws.Range("L1:AB40")

        On Error Resume Next
        Set rng1 = rng2.SpecialCells(xlBlanks)
        on error goto 0

        if not rng1 is nothing then
            Application.Iteration = True
            rng1.FormulaR1C1 = "=AVERAGE(R[-1]C,R[1]C)"
            Application.Iteration = False
            rng2.Value = rng2.Value
        end if    
    Next ws 
End Sub

1 个答案:

答案 0 :(得分:1)

如果将要跳过的工作表名称添加到worksheetsToSkip = array("...行(如下),则下面的代码应跳过所述工作表。

Option Explicit

Sub FillBlanks()

    Dim ws As Worksheet
    Dim rng1 As Range
    Dim rng2 As Range

    Dim worksheetsToSkip As Variant
    worksheetsToSkip = Array("Aggregated", "Collated Results", "Template", "End")

    For Each ws In Worksheets
        If IsError(Application.Match(ws.Name, worksheetsToSkip, 0)) Then
             Set rng2 = ws.Range("L1:AB40")

             On Error Resume Next
             Set rng1 = rng2.SpecialCells(xlBlanks)
             On Error GoTo 0

            If Not rng1 Is Nothing Then
                 Application.Iteration = True
                 rng1.FormulaR1C1 = "=AVERAGE(R[-1]C,R[1]C)"
                 Application.Iteration = False
                 rng2.Value = rng2.Value
            End If
        End If
    Next ws
End Sub