Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对于每个循环错误VBA_Vba_Excel_For Loop - Fatal编程技术网

对于每个循环错误VBA

对于每个循环错误VBA,vba,excel,for-loop,Vba,Excel,For Loop,我已经编写了一个代码,可以执行以下操作: 清除定义的单元格范围并删除任何背景色 (第一个选项卡除外) 重置选项卡颜色 我从这里得到了一个暗示: 这是: Sub ClearAll() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets If ws.Index <> 1 Then Call clear(ws) End If Next E

我已经编写了一个代码,可以执行以下操作:

  • 清除定义的单元格范围并删除任何背景色
    (第一个选项卡除外)
  • 重置选项卡颜色
我从这里得到了一个暗示:

这是:

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
Sub ClearAll()
将ws设置为工作表
对于ActiveWorkbook.Worksheets中的每个ws
如果ws.Index为1,那么
呼叫清除(ws)
如果结束
下一个
端接头
子清除(ws-As工作表)
使用ActiveSheet
.Tab.ColorIndex=xlColorIndexNone
.Range(单元格(5,7),单元格(.Cells(.Rows.Count,“N”).End(xlUp).Row-1,14)).ClearContents
.Range(单元格(5,1),单元格(.Cells(.Rows.Count,“D”).End(xlUp).Row-1,4)).ClearContents
.Range(单元格(5,7),单元格(.Cells(.Rows.Count,“N”).End(xlUp).Row-1,14)).Interior.ColorIndex=0
.Range(单元格(5,1),单元格(.Cells(.Rows.Count,“D”).End(xlUp).Row-1,4)).Interior.ColorIndex=0
以
端接头
但运行它之后,我得到了一个运行时错误“1004”:应用程序定义的错误或对象定义的错误

我修改了密码。这看起来很糟糕,但至少它起作用了

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
Sub ClearAll1()
作为整数的Dim quantWs
将a变暗为整数
quantWs=ActiveWorkbook.Worksheets.Count
a=2

执行时,您需要在清除子项中使用ws对象而不是ActiveSheet

我会将ActiveWorkbook更改为此工作簿,因为如果您打开了多个工作簿,则excel会得到错误的工作簿。如果您输入原因,您可以阅读它:

Sub ClearAll()
将ws设置为工作表
对于此工作簿中的每个ws。工作表
如果ws.Index为1,那么
呼叫清除(ws)
如果结束
下一个
端接头
子清除(ws-As工作表)
与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
以
端接头

您需要在清除子项中使用ws对象而不是ActiveSheet

我会将ActiveWorkbook更改为此工作簿,因为如果您打开了多个工作簿,则excel会得到错误的工作簿。如果您输入原因,您可以阅读它:

Sub ClearAll()
将ws设置为工作表
对于此工作簿中的每个ws。工作表
如果ws.Index为1,那么
呼叫清除(ws)
如果结束
下一个
端接头
子清除(ws-As工作表)
与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
以
端接头

您只需使用ActiveSheet将
更改为使用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

您只需将使用ActiveSheet的
更改为使用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

范围内的单元格需要进行限定。
.Range(.cells(5,7),.cells(.cells(.Rows.Count,“N”).End(xlUp)。Row-1,14))
非常感谢大家的帮助。现在似乎工作得很好。范围内的单元格需要进行限定。
.Range(.cells(5,7),.cells(.cells(.Rows.Count,“N”).End(xlUp)。Row-1,14))
非常感谢你们的帮助,伙计们。现在似乎工作得很好。