Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA - Cannot Set Color to RGB(#,#,#) value for one cell when others work? 1

Status
Not open for further replies.

kjv1611

New member
Jul 9, 2003
10,758
US
I put this together last week, and in what I remember, it worked fine.

Today, suddenly, in one cell, it seems to not work. It fails at Cell(5,5). I've highlighted the line of code that is throwing the error. It's within my error handler:
[CODE VBA]Sub MarkBadPaths()
On Error GoTo ErrHandler
' Purpose: Highlight bad paths as dark gray to show not valid

Dim wb As Workbook
Dim ws As Worksheet
Dim x As Integer 'row
Dim y As Integer 'column

Set wb = ActiveWorkbook
Set ws = wb.Worksheets("CrossTableUpdates")

For x = 2 To ws.Range("A65000").End(xlUp).Row
For y = 5 To 8 'can be changed later to be more flexible
If y = 6 Then 'skip this column for now, Dir() function doesn't like named network shares
Else
ws.Cells(x, y).Select
If Dir(ws.Cells(x, y).Value, vbDirectory) = vbNullString Then
' If Dir() evaluates to vbNullString (""), then it's an invalid folder/directory address.
ws.Cells(x, y).Interior.Color = RGB(166, 166, 166) 'Gray
Debug.Print ws.Cells(x, y).Address & "is not valid"
End If
End If
Next y

Next x

ExitSub:
Set ws = Nothing
Set wb = Nothing
Exit Sub

ErrHandler:
If Err.Number = 52 Then 'Bad file name or number
[highlight #FCE94F]ws.Cells(x, y).Color = RGB(166, 166, 166)[/highlight]
Resume Next
Else
MsgBox "Error number = " & Err.Number & _
Err.Description _
, vbCritical, "Error"
Resume ExitSub
End If

End Sub[/CODE]

The step basically says if the code throws me a bad file name or number error, I color that cell dark gray and move on to the next. The idea of the code is to show at a glance what network paths are valid vs invalid.

Thanks for any thoughts.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
I am not sure, but a little research showed me that ws.Cells(x,y).Interior.Color = RGB(166,166,166) should work.
 
Hmm... I wonder if I somehow undid that portion. That's right - I believe it does require Interior.Color which I did have in other places in code. Thanks! I'll verify in a little bit and post back.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Indeed, that was the problem. Thanks again.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top