Can someone tell me if I'm coding this poorly. This select seems to fail sometimes. By this I mean I haven't changed the xls itself but I may just examine the data and then rerun the calling routine and it may fail with an error of "select method of range class fails"
My goal with this routine is to change the color of a column of data. From row 1 through the last row used. When this runs
cnum = 16 <-- which is the column number I want to highlight
lrow = 1418 <-- which is the last row with data
Sub HighlightColumn(colname As String, sheet As Worksheet, color As Long)
Dim cnum As Integer
Dim lrow As Long
Dim lcol As Integer
Dim r As Range
lcol = sheet.UsedRange.Columns.Count
lrow = RowCount(sheet)
'get column number cnum with the name colname
For i = 1 To lcol
If (sheet.Cells(1, i).Value = colname) Then cnum = i
Next i
'create range
Set r = sheet.Range(sheet.Cells(1, cnum), sheet.Cells(lrow, cnum))
r.Select 'this is the statement that fails
'set the color
Selection.Interior.color = color
End Sub