I am using Active Sheet object of excel to loop through rows and columns.
I need to change the background color of a cell but getting Application defined or object- defined error on this line
ws.Cells(rw, 4).Interior.ColorIndex = 0
This is the code I am using
Dim ws As Worksheet
Set ws = ActiveSheet
With ws
For rw = 7 To ws.Rows.Count
For col = 2 To 12
'Check the first column and if null then exit
If ws.Cells(rw, 2) = "" Then
Exit Sub
End If
'Check if article code is less than eight digits
If Len(ws.Cells(rw, 4)) < 8 Then
ws.Cells(rw, 4).Interior.ColorIndex = 3
Else
ws.Cells(rw, 4).Interior.ColorIndex = 0
End If
Next col
Next rw
End With
Any thoughts?
There is nothing wrong with the code. You are getting that error because your worksheet is protected. You need to unprotect the sheet and then change the cell colors. When you have finished changing the colors, you can then reprotect it. See this code.