alfonsoto1
Board Regular
- Joined
- Nov 25, 2004
- Messages
- 107
How do i go about coloring an active cell aaaaaaayellow?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = xlNone
ActiveCell.Interior.ColorIndex = 6
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Cells.Interior.ColorIndex = xlNone
ActiveCell.Interior.ColorIndex = 6
End Sub
just curious what makes this one better? what does ByVal Sh As Object do differently?Delete that macro and put the next one but in ThisWorkbook events, it will work on any sheet.
VBA Code:Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Cells.Interior.ColorIndex = xlNone ActiveCell.Interior.ColorIndex = 6 End Sub
It works in any worksheet in the workbook (provided it is placed as explained by Dante) - which is what the OP asked for.just curious what makes this one better? what does ByVal Sh As Object do differently?
Yeah i know the location of the code is the only thing that matters most for what OP needed so it works in all sheets. But just wondering why he made that new code and told him to delete what he already had, wondering what made it better/more efficient?It works in any worksheet in the workbook (provided it is placed as explained by Dante) - which is what the OP asked for.
But location itself is not enough. If the OP's original code was simply moved to the ThisWorkbook module it would not work.Yeah i know the location of the code is the only thing that matters most for what OP needed so it works in all sheets.
As per the above, the original code would only work in the worksheet whose module it was in. The OP's question was how to make it work in all the worksheets.But just wondering why he made that new code ..
If the original code was not removed then there would be a code in that worksheet module clearing colour from all cells in the worksheet and then applying colour to the active cell and there would be code in ThisWorkbook clearing colour from all the cells in the worksheet and then applying colour to the active cell.... and told him to delete what he already had,
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
To worksheet code module.How do i go about coloring an active cell aaaaaaayellow?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cf As FormatCondition, r As Range
For Each cf In Me.Cells.FormatConditions
If cf.Type = 2 Then
If cf.Formula1 Like "=ADDRESS(*" Then cf.Delete
End If
Next
For Each r In Target
r.FormatConditions.Add 2, Formula1:="=address(row(),column())=""" & r.Address & """"
r.FormatConditions(1).Interior.Color = vbYellow
r.FormatConditions(1).StopIfTrue = False
Next
End Sub