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
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