Code Error on setting range

jeffmoseler

Well-known Member
Joined
Jul 16, 2004
Messages
540
I have the following code to adjust the color of a tab if certain conditions are present.

Code:
Public Sub CheckForTabColorSuspects1019()
    Dim i, vk As Long
    Sheets("Suspects 10 - 19").Tab.ColorIndex = xlColorIndexNone
    vk = Application.WorksheetFunction.CountA(Range("A12:A1000"))
    If vk = 0 Or vk = Empty Then
        Exit Sub
    End If
    vk = vk + 11
    i = Sheets("Suspects 10 - 19").Range("A11:AZ11").Find("Added", Range("A11"), xlFormulas, xlWhole, xlByColumns, xlNext).Column
    i2 = Sheets("Suspects 10 - 19").Range("A11:AZ11").Find("App?", Range("A11"), xlFormulas, xlWhole, xlByColumns, xlNext).Column
    Dim rng As Range
    Dim vMaxDate As Date

For j = 12 To vk
    Set rng = Sheets("Suspects 10 - 19").Range(Cells(j, i), Cells(j, i2))
    vMaxDate = Application.WorksheetFunction.Max(rng)
    
    If vMaxDate <= Date - 14 Then
        Sheets("Suspects 10 - 19").Tab.ColorIndex = 3
    Exit Sub
    End If
    
    If vMaxDate <= Date - 7 Then
        Sheets("Suspects 10 - 19").Tab.ColorIndex = 6
    End If

Next j

End Sub

Everytime it runs is errors on this line:
Code:
Set rng = Sheets("Suspects 10 - 19").Range(Cells(j, i), Cells(j, i2))

Saying:
"Application-defined or object-defined error"

I call it when there is a change on the sheet and when the worksheet opens. And it seems to be completely random. Sometimes it causes an error and sometimes it runs fine. Any thoughts?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this.
Code:
With Sheets("Suspects 10 - 19")
    Set rng = .Range(.Cells(j, i), .Cells(j, i2))
End With
 
Upvote 0

Forum statistics

Threads
1,214,837
Messages
6,121,883
Members
449,057
Latest member
Moo4247

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top