【问题标题】:For Each loop error VBA对于每个循环错误 VBA
【发布时间】:2018-02-13 11:19:55
【问题描述】:

我已经写了一个代码来做接下来的事情:

  • 清除定义的单元格范围并删除任何背景颜色
    (第一个标签除外);
  • 重置标签颜色;

我从这里得到了提示:Excel VBA For Each Worksheet Loop

这里是:

Sub ClearAll()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Index <> 1 Then
            Call clear(ws)
        End If
    Next
End Sub
Sub clear(ws As Worksheet)
    With ActiveSheet
        .Tab.ColorIndex = xlColorIndexNone
        .Range(Cells(5, 7), Cells(.Cells(.Rows.Count, "N").End(xlUp).Row - 1, 14)).ClearContents
        .Range(Cells(5, 1), Cells(.Cells(.Rows.Count, "D").End(xlUp).Row - 1, 4)).ClearContents
        .Range(Cells(5, 7), Cells(.Cells(.Rows.Count, "N").End(xlUp).Row - 1, 14)).Interior.ColorIndex = 0
        .Range(Cells(5, 1), Cells(.Cells(.Rows.Count, "D").End(xlUp).Row - 1, 4)).Interior.ColorIndex = 0
    End With
End Sub

但运行后我得到一个错误 Run-time error '1004': Application-defined or object defined error.

我修改了代码。它看起来很糟糕:(。但至少它有效。

Sub ClearAll1()
    Dim quantWs As Integer
    Dim a As Integer

    quantWs = ActiveWorkbook.Worksheets.Count
    a = 2
    Do While a <= quantWs
        Worksheets(a).Activate
        ActiveSheet.Tab.ColorIndex = xlColorIndexNone
        Call clear
        a = a + 1
    Loop
End Sub
Sub clear(ws As Worksheet)
    With ActiveSheet
        .Tab.ColorIndex = xlColorIndexNone
        .Range(Cells(5, 7), Cells(.Cells(.Rows.Count, "N").End(xlUp).Row - 1, 14)).ClearContents
        .Range(Cells(5, 1), Cells(.Cells(.Rows.Count, "D").End(xlUp).Row - 1, 4)).ClearContents
        .Range(Cells(5, 7), Cells(.Cells(.Rows.Count, "N").End(xlUp).Row - 1, 14)).Interior.ColorIndex = 0
        .Range(Cells(5, 1), Cells(.Cells(.Rows.Count, "D").End(xlUp).Row - 1, 4)).Interior.ColorIndex = 0
    End With
End Sub

有人能指出第一个示例中出了什么问题吗?因为工作版太适得其反了……

感谢您的帮助。

【问题讨论】:

    标签: vba excel for-loop


    【解决方案1】:

    您需要在 Clear Sub

    中使用 ws 对象而不是 ActiveSheet

    我会将 ActiveWorkbook 更改为 ThisWorkbook,因为如果您打开的工作簿不止一个,则 Excel 会得到错误的工作簿。如果你 Intrestet 为什么会这样,你可以读她:VBA ACTIVEWORKBOOK VS THISWORKBOOK

    Sub ClearAll()
            Dim ws As Worksheet
            For Each ws In ThisWorkbook.Worksheets
                If ws.Index <> 1 Then
                    Call clear(ws)
                End If
            Next
        End Sub
        Sub clear(ws As Worksheet)
            With ws 
                .Tab.ColorIndex = xlColorIndexNone
                .Range(.Cells(5, 7), .Cells(.Cells(.Rows.Count, "N").End(xlUp).Row - 1, 14)).ClearContents
                .Range(.Cells(5, 1), .Cells(.Cells(.Rows.Count, "D").End(xlUp).Row - 1, 4)).ClearContents
                .Range(.Cells(5, 7), .Cells(.Cells(.Rows.Count, "N").End(xlUp).Row - 1, 14)).Interior.ColorIndex = 0
                .Range(.Cells(5, 1), .Cells(.Cells(.Rows.Count, "D").End(xlUp).Row - 1, 4)).Interior.ColorIndex = 0
            End With
        End Sub
    

    【讨论】:

    • 范围内的单元格需要被限定。 .Range(.Cells(5, 7), .Cells(.Cells(.Rows.Count, "N").End(xlUp).Row - 1, 14))
    • 非常感谢各位的帮助。现在似乎工作得很好。
    【解决方案2】:

    您只需将With ActiveSheet 更改为With ws,但我认为这更容易阅读:

    Sub clear(ws As Worksheet)
        With ws
            .Tab.ColorIndex = xlColorIndexNone
            With .Range(.Cells(5, 7), .Cells(.Rows.Count, "N").End(xlUp).Offset(-1))
                .ClearContents
                .Interior.ColorIndex = 0
            End With
    
            With .Range("A5", .Range("D" & .Rows.Count).End(xlUp).Offset(-1)))
                .ClearContents
                .Interior.ColorIndex = 0
            End With
    
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2018-10-09
      • 2016-02-26
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多