BangBoy
BangBoy

Reputation: 45

For Each loop error VBA

I have written a code which do the next things:

I took a hint from here: Excel VBA For Each Worksheet Loop

Here it is:

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

But after running it I got an error Run-time error '1004': Application-defined or object defined error.

I have altered the code. It looks terrible :(. But at least it works.

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

Could someone point what gone wrong in the first sample? Because the working version is so counterproductive...

Thank you for any help.

Upvotes: 1

Views: 327

Answers (2)

user6432984
user6432984

Reputation:

You simply need to change With ActiveSheet to With ws but I think this is much easier to read:

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

Upvotes: 2

Moosli
Moosli

Reputation: 3285

You need to use the ws Object and not ActiveSheet in your Clear Sub

And i would Change ActiveWorkbook to ThisWorkbook beacuse if you have more then one workbook Open there are changes that excel will get the Wrong Workbook. If you Intrestet why this is so, you can read it her: 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

Upvotes: 3

Related Questions