Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 191
Default Highlight Cells

I would like to highlight a line of cells when the cell is selected. I tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Highlight Cells

That code works for me on a brand new workbook. Is your worksheet protected???
--
HTH...

Jim Thomlinson


"ranswrt" wrote:

I would like to highlight a line of cells when the cell is selected. I tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Highlight Cells

I'm not completely sure what range you are trying to color... is it a 3
columns wide by 6 rows deep with the active cell in the upper left of that
rectangle? If so, is this what you want to do...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Target.Resize(6, 3).Interior.ColorIndex = 36
End Sub

Rick


"ranswrt" wrote in message
...
I would like to highlight a line of cells when the cell is selected. I
tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Highlight Cells

Okay, I see what you are trying to highlight now. Try this event code
instead...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Cells(Target.Row, 3).Resize(1, 6).Interior.ColorIndex = 36
End Sub

Note that it removes the previous highlight (I tried to guess at your
intent). If you do not want that feature, delete the first line (the
UsedRange one) and leave just the last line.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
I'm not completely sure what range you are trying to color... is it a 3
columns wide by 6 rows deep with the active cell in the upper left of that
rectangle? If so, is this what you want to do...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Target.Resize(6, 3).Interior.ColorIndex = 36
End Sub

Rick


"ranswrt" wrote in message
...
I would like to highlight a line of cells when the cell is selected. I
tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 191
Default Highlight Cells

It was protected that was the problem.
Thank you

"Jim Thomlinson" wrote:

That code works for me on a brand new workbook. Is your worksheet protected???
--
HTH...

Jim Thomlinson


"ranswrt" wrote:

I would like to highlight a line of cells when the cell is selected. I tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 191
Default Highlight Cells

I tried your code but I have a problem with the 'usedrange'. It is changing
the color on a larger range instead of the previous range. I am not familiar
on how to use that code. How do I change it to just the previous range that
had the color change?


"Rick Rothstein (MVP - VB)" wrote:

Okay, I see what you are trying to highlight now. Try this event code
instead...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Cells(Target.Row, 3).Resize(1, 6).Interior.ColorIndex = 36
End Sub

Note that it removes the previous highlight (I tried to guess at your
intent). If you do not want that feature, delete the first line (the
UsedRange one) and leave just the last line.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
I'm not completely sure what range you are trying to color... is it a 3
columns wide by 6 rows deep with the active cell in the upper left of that
rectangle? If so, is this what you want to do...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Target.Resize(6, 3).Interior.ColorIndex = 36
End Sub

Rick


"ranswrt" wrote in message
...
I would like to highlight a line of cells when the cell is selected. I
tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Highlight Cells

Make it:

Me.UsedRange.Interior.ColorIndex = xlNone

Me is the object that owns the code--in this case, it's the worksheet with the
code.

ranswrt wrote:

I tried your code but I have a problem with the 'usedrange'. It is changing
the color on a larger range instead of the previous range. I am not familiar
on how to use that code. How do I change it to just the previous range that
had the color change?

"Rick Rothstein (MVP - VB)" wrote:

Okay, I see what you are trying to highlight now. Try this event code
instead...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Cells(Target.Row, 3).Resize(1, 6).Interior.ColorIndex = 36
End Sub

Note that it removes the previous highlight (I tried to guess at your
intent). If you do not want that feature, delete the first line (the
UsedRange one) and leave just the last line.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
I'm not completely sure what range you are trying to color... is it a 3
columns wide by 6 rows deep with the active cell in the upper left of that
rectangle? If so, is this what you want to do...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Target.Resize(6, 3).Interior.ColorIndex = 36
End Sub

Rick


"ranswrt" wrote in message
...
I would like to highlight a line of cells when the cell is selected. I
tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks




--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Highlight Cells

That gets a bit tricky. You need to store the range in a static variable.
Here is some code that I use, which you can probalby adapt without too much
trouble...

*In a standard module****
Option Explicit

'***This code has the side effect that you can not copy on any sheet it is
run against

Public HighlightRow As clsHighlightRows

Public Sub Auto_Open()
Set HighlightRow = New clsHighlightRows

HighlightRow.AddSheet Sheet1
HighlightRow.AddSheet Sheet2

End Sub

Public Sub Auto_Close()
Set HighlightRow = Nothing
End Sub

*in a Class Module********
Option Explicit
Private HighlightSheets As New Collection
Private WithEvents xlApp As Excel.Application
Private rngOldTarget As Range

Private Sub Class_Initialize()
Set xlApp = Excel.Application
End Sub


Private Sub Class_Terminate()
Set xlApp = Nothing
Set HighlightSheets = Nothing
End Sub

Private Sub xlApp_SheetActivate(ByVal Sh As Object)
'Initialize the last cell to the current cell of this sheet
Set rngOldTarget = ActiveCell

'Highlight the Font of the current cell if necessary
Call xlApp_SheetSelectionChange(Sh, rngOldTarget)
End Sub

Private Sub xlApp_SheetSelectionChange(ByVal Sh As Object, ByVal Target As
Range)
Dim wks As Worksheet

On Error Resume Next
Set wks = HighlightSheets.Item(Sh.Name)
On Error GoTo 0

If Not wks Is Nothing Then Call HighlightRow(Sh, Target)
End Sub


Public Function AddSheet(ByVal wks As Worksheet)
Call HighlightSheets.Add(wks, wks.Name)
End Function

Public Function RemoveSheet(ByVal wks As Worksheet)
Call HighlightSheets.Remove(wks.Name)
End Function

Public Property Get Items() As Collection
Set Items = HighlightSheets
End Property

Private Sub HighlightRow(ByVal Sh As Object, ByVal Target As Range)

If Not (rngOldTarget Is Nothing) Then
rngOldTarget.EntireRow.Interior.ColorIndex = xlNone
End If
'Set Last cell = Current cell
Set rngOldTarget = Target
Target.EntireRow.Interior.ColorIndex = 36
End Sub
--
HTH...

Jim Thomlinson


"ranswrt" wrote:

I tried your code but I have a problem with the 'usedrange'. It is changing
the color on a larger range instead of the previous range. I am not familiar
on how to use that code. How do I change it to just the previous range that
had the color change?


"Rick Rothstein (MVP - VB)" wrote:

Okay, I see what you are trying to highlight now. Try this event code
instead...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Cells(Target.Row, 3).Resize(1, 6).Interior.ColorIndex = 36
End Sub

Note that it removes the previous highlight (I tried to guess at your
intent). If you do not want that feature, delete the first line (the
UsedRange one) and leave just the last line.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
I'm not completely sure what range you are trying to color... is it a 3
columns wide by 6 rows deep with the active cell in the upper left of that
rectangle? If so, is this what you want to do...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Target.Resize(6, 3).Interior.ColorIndex = 36
End Sub

Rick


"ranswrt" wrote in message
...
I would like to highlight a line of cells when the cell is selected. I
tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 191
Default Highlight Cells

Thanks I'll work on those.

"ranswrt" wrote:

I tried your code but I have a problem with the 'usedrange'. It is changing
the color on a larger range instead of the previous range. I am not familiar
on how to use that code. How do I change it to just the previous range that
had the color change?


"Rick Rothstein (MVP - VB)" wrote:

Okay, I see what you are trying to highlight now. Try this event code
instead...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Cells(Target.Row, 3).Resize(1, 6).Interior.ColorIndex = 36
End Sub

Note that it removes the previous highlight (I tried to guess at your
intent). If you do not want that feature, delete the first line (the
UsedRange one) and leave just the last line.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
I'm not completely sure what range you are trying to color... is it a 3
columns wide by 6 rows deep with the active cell in the upper left of that
rectangle? If so, is this what you want to do...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Target.Resize(6, 3).Interior.ColorIndex = 36
End Sub

Rick


"ranswrt" wrote in message
...
I would like to highlight a line of cells when the cell is selected. I
tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default Highlight Cells

Mr Jim,
What is the simplest way to higlighted the
edited cell?


"Jim Thomlinson" wrote in message
...
That gets a bit tricky. You need to store the range in a static variable.
Here is some code that I use, which you can probalby adapt without too

much
trouble...

*In a standard module****
Option Explicit

'***This code has the side effect that you can not copy on any sheet it is
run against

Public HighlightRow As clsHighlightRows

Public Sub Auto_Open()
Set HighlightRow = New clsHighlightRows

HighlightRow.AddSheet Sheet1
HighlightRow.AddSheet Sheet2

End Sub

Public Sub Auto_Close()
Set HighlightRow = Nothing
End Sub

*in a Class Module********
Option Explicit
Private HighlightSheets As New Collection
Private WithEvents xlApp As Excel.Application
Private rngOldTarget As Range

Private Sub Class_Initialize()
Set xlApp = Excel.Application
End Sub


Private Sub Class_Terminate()
Set xlApp = Nothing
Set HighlightSheets = Nothing
End Sub

Private Sub xlApp_SheetActivate(ByVal Sh As Object)
'Initialize the last cell to the current cell of this sheet
Set rngOldTarget = ActiveCell

'Highlight the Font of the current cell if necessary
Call xlApp_SheetSelectionChange(Sh, rngOldTarget)
End Sub

Private Sub xlApp_SheetSelectionChange(ByVal Sh As Object, ByVal Target As
Range)
Dim wks As Worksheet

On Error Resume Next
Set wks = HighlightSheets.Item(Sh.Name)
On Error GoTo 0

If Not wks Is Nothing Then Call HighlightRow(Sh, Target)
End Sub


Public Function AddSheet(ByVal wks As Worksheet)
Call HighlightSheets.Add(wks, wks.Name)
End Function

Public Function RemoveSheet(ByVal wks As Worksheet)
Call HighlightSheets.Remove(wks.Name)
End Function

Public Property Get Items() As Collection
Set Items = HighlightSheets
End Property

Private Sub HighlightRow(ByVal Sh As Object, ByVal Target As Range)

If Not (rngOldTarget Is Nothing) Then
rngOldTarget.EntireRow.Interior.ColorIndex = xlNone
End If
'Set Last cell = Current cell
Set rngOldTarget = Target
Target.EntireRow.Interior.ColorIndex = 36
End Sub
--
HTH...

Jim Thomlinson


"ranswrt" wrote:

I tried your code but I have a problem with the 'usedrange'. It is

changing
the color on a larger range instead of the previous range. I am not

familiar
on how to use that code. How do I change it to just the previous range

that
had the color change?


"Rick Rothstein (MVP - VB)" wrote:

Okay, I see what you are trying to highlight now. Try this event code
instead...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Cells(Target.Row, 3).Resize(1, 6).Interior.ColorIndex = 36
End Sub

Note that it removes the previous highlight (I tried to guess at your
intent). If you do not want that feature, delete the first line (the
UsedRange one) and leave just the last line.

Rick


"Rick Rothstein (MVP - VB)"

wrote in
message ...
I'm not completely sure what range you are trying to color... is it

a 3
columns wide by 6 rows deep with the active cell in the upper left

of that
rectangle? If so, is this what you want to do...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UsedRange.Interior.ColorIndex = xlNone
Target.Resize(6, 3).Interior.ColorIndex = 36
End Sub

Rick


"ranswrt" wrote in message
...
I would like to highlight a line of cells when the cell is selected.

I
tried
the following code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim ycell As Range

Set ycell = ActiveCell.EntireRow.Cells(3)
Set rng = Range(ycell, ycell.Offset(0, 5))
rng.Interior.ColorIndex = 36

End Sub

but I get an error that says 'Unable to set ColorIndex property of

the
interior class'.
What am I doing wrong here? or is there a better way to do this?
Thanks





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to highlight more related cells if cell highlight Jon Excel Discussion (Misc queries) 5 December 21st 08 01:06 PM
How to highlight cells... Mare Excel Discussion (Misc queries) 3 January 28th 06 01:09 PM
Highlight Cells EMoe[_74_] Excel Programming 6 November 9th 05 06:26 PM
Highlight cells with ctrl-click but only un-highlight one cell hagan Excel Discussion (Misc queries) 5 May 27th 05 06:45 PM
Highlight all cells, except top row? Reedi Excel Programming 1 October 3rd 03 10:03 PM


All times are GMT +1. The time now is 03:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"