I have written a code which do the next things:
- clears defined ranges of cells and remove any background color
(except first tab); - resets the tab color;
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.