ExcelChampion
Well-known Member
- Joined
- Aug 12, 2005
- Messages
- 976
I was asked to implement this code, which highlights the activerow (I don't know where it came from so I can't give credit):-
Problem is, if you Insert a row the code fails to highlight anything. Secondly, trying to insert a row with a macro fails miserably. I tried like heck to edit the code so that it would work, but it either interfered with my macro or just failed miserably any time I made a small change to it.
So, I scrapped it all together and thought I'd share what I did, since it seems so simple, yet elegant (forgive me if everyone already knew of this).
1. Go to Insert/Name/Define. For the Name, I used, "myCellFormat". For the Reference I used: =Get.Cell(2)=Row()
2. Now apply Conditional Format:- Select a range and go to Format/Conditional formatting. Choose "Formula is" and use the formula, =myCellformat, and then select the Light Yellow color pattern and click Ok and then click Ok again.
3. The only thing left to do is make sure that it calculates. The formulas will only update when forced to calculate so I used a Worksheet_SelectionChange event to fire "Calculate" to force the calculation. After it calculates, the activerow will turn Light Yellow.
I think it's neat in it's simplicity.
Code:
Function InRange(Range1 As Range, Range2 As Range) As Boolean
' returns True if Range1 is within Range2
Dim InterSectRange As Range
If ActiveSheet.Name = "Sum" Then
Set InterSectRange = Application.Intersect(Range1, Range2)
InRange = Not InterSectRange Is Nothing
Set InterSectRange = Nothing
Else
End If
End Function
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
If InRange(ActiveCell, Range("Database")) Then
Application.ScreenUpdating = False
ActiveSheet.Unprotect "password"
ThisWorkbook.Unprotect "password"
Const cnNUMCOLS As Long = 13
Const cnHIGHLIGHTCOLOR As Long = 36 'default lt. yellow
Static rOld As Range
Static nColorIndices(1 To cnNUMCOLS) As Long
Dim i As Long
If Not rOld Is Nothing Then 'Restore color indices
With rOld.Cells
If .Row = ActiveCell.Row Then Exit Sub 'same row, don't restore
For i = 1 To cnNUMCOLS
.Item(i).Interior.ColorIndex = nColorIndices(i)
Next i
End With
End If
Set rOld = Cells(ActiveCell.Row, 1).Resize(1, cnNUMCOLS)
With rOld
For i = 1 To cnNUMCOLS
nColorIndices(i) = .Item(i).Interior.ColorIndex
Next i
.Interior.ColorIndex = cnHIGHLIGHTCOLOR
End With
Else
End If
Application.ScreenUpdating = True
ActiveSheet.Protect "password"
ThisWorkbook.Protect "password"
End Sub
Problem is, if you Insert a row the code fails to highlight anything. Secondly, trying to insert a row with a macro fails miserably. I tried like heck to edit the code so that it would work, but it either interfered with my macro or just failed miserably any time I made a small change to it.
So, I scrapped it all together and thought I'd share what I did, since it seems so simple, yet elegant (forgive me if everyone already knew of this).
1. Go to Insert/Name/Define. For the Name, I used, "myCellFormat". For the Reference I used: =Get.Cell(2)=Row()
2. Now apply Conditional Format:- Select a range and go to Format/Conditional formatting. Choose "Formula is" and use the formula, =myCellformat, and then select the Light Yellow color pattern and click Ok and then click Ok again.
3. The only thing left to do is make sure that it calculates. The formulas will only update when forced to calculate so I used a Worksheet_SelectionChange event to fire "Calculate" to force the calculation. After it calculates, the activerow will turn Light Yellow.
I think it's neat in it's simplicity.