PC Review


Reply
Thread Tools Rate Thread

Change background colour of selected cells

 
 
=?Utf-8?B?UmVkbGVn?=
Guest
Posts: n/a
 
      28th Mar 2007
I wish to change the background colour of any selected cell on entry and
revert to original colour (none) on exit.
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UmVkbGVn?=
Guest
Posts: n/a
 
      29th Mar 2007
Thanks for the help so far Greg.

In both options I get a runtime error "Unable to set the ColorIndex property
of the interior class"

"Greg Wilson" wrote:

> This version appears to adequately compensate for the problem described if
> you need to only copy and paste values. You will need to set a reference to
> the Microsoft Forms 2.0 Object Library for it to work. Paste all of the below
> to the worksheet's code module. Minimal testing and never used personally:-
>
> Dim copyval As String
>
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> Cells.FormatConditions.Delete
> If Len(copyval) > 0 Then CopyToClip (copyval)
> With Target
> If .Count > 1 Then Exit Sub
> copyval = .Value
> .FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
> .FormatConditions(1).Interior.ColorIndex = 19
> End With
> End Sub
>
> Private Sub CopyToClip(txt)
> Dim DataObj As DataObject
> Set DataObj = New DataObject
> DataObj.SetText txt
> DataObj.PutInClipboard
> Set DataObj = Nothing
> End Sub
>
> Regards,
> Greg
>
>
>

 
Reply With Quote
 
=?Utf-8?B?R3JlZyBXaWxzb24=?=
Guest
Posts: n/a
 
      30th Mar 2007
The worksheet is presumably protected. The following code unprotects, does
the job, then reprotects. If you are not using a password then you can delete
the password argument that follows the unprotect and protect statements. If
you are using a password then you need to substitute it for "mypassword":

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Me.Unprotect "mypassword"
Cells.FormatConditions.Delete
If Len(copyval) > 0 Then CopyToClip (copyval)
With Target
If .Count > 1 Then Exit Sub
copyval = .Value
..FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
..FormatConditions(1).Interior.ColorIndex = 19
End With
Me.Protect "mypassword"
End Sub

Alternatively, if you are using xl2003 or later, there is probably
protection option that allows conditional formating changes thus letting you
avoid the above changes. I don't have access to xl2003 at this time to check
this out.

Regards,
Greg

"Redleg" wrote:

> Thanks for the help so far Greg.
>
> In both options I get a runtime error "Unable to set the ColorIndex property
> of the interior class"
>
> "Greg Wilson" wrote:
>
> > This version appears to adequately compensate for the problem described if
> > you need to only copy and paste values. You will need to set a reference to
> > the Microsoft Forms 2.0 Object Library for it to work. Paste all of the below
> > to the worksheet's code module. Minimal testing and never used personally:-
> >
> > Dim copyval As String
> >
> > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> > Cells.FormatConditions.Delete
> > If Len(copyval) > 0 Then CopyToClip (copyval)
> > With Target
> > If .Count > 1 Then Exit Sub
> > copyval = .Value
> > .FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
> > .FormatConditions(1).Interior.ColorIndex = 19
> > End With
> > End Sub
> >
> > Private Sub CopyToClip(txt)
> > Dim DataObj As DataObject
> > Set DataObj = New DataObject
> > DataObj.SetText txt
> > DataObj.PutInClipboard
> > Set DataObj = Nothing
> > End Sub
> >
> > Regards,
> > Greg
> >
> >
> >

 
Reply With Quote
 
=?Utf-8?B?R3JlZyBXaWxzb24=?=
Guest
Posts: n/a
 
      30th Mar 2007
I suggest you substitute the following. The problem with the previous version
is that it will protect the wks even if it wasn't protected to begin with.
This one will only protect it if it was already protected. Otherwise it could
be a nuisance depending on what you are doing. The same comments hold
regarding the password.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim protected As Boolean
protected = Me.ProtectContents
If protected Then Me.Unprotect ' "mypassword"
Cells.FormatConditions.Delete
If Len(copyval) > 0 Then CopyToClip (copyval)
With Target
If .Count > 1 Then Exit Sub
copyval = .Value
..FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
..FormatConditions(1).Interior.ColorIndex = 19
End With
If protected Then Me.Protect ' "mypassword"
End Sub

Greg

"Redleg" wrote:

> Thanks for the help so far Greg.
>
> In both options I get a runtime error "Unable to set the ColorIndex property
> of the interior class"
>
> "Greg Wilson" wrote:
>
> > This version appears to adequately compensate for the problem described if
> > you need to only copy and paste values. You will need to set a reference to
> > the Microsoft Forms 2.0 Object Library for it to work. Paste all of the below
> > to the worksheet's code module. Minimal testing and never used personally:-
> >
> > Dim copyval As String
> >
> > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> > Cells.FormatConditions.Delete
> > If Len(copyval) > 0 Then CopyToClip (copyval)
> > With Target
> > If .Count > 1 Then Exit Sub
> > copyval = .Value
> > .FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
> > .FormatConditions(1).Interior.ColorIndex = 19
> > End With
> > End Sub
> >
> > Private Sub CopyToClip(txt)
> > Dim DataObj As DataObject
> > Set DataObj = New DataObject
> > DataObj.SetText txt
> > DataObj.PutInClipboard
> > Set DataObj = Nothing
> > End Sub
> >
> > Regards,
> > Greg
> >
> >
> >

 
Reply With Quote
 
=?Utf-8?B?UmVkbGVn?=
Guest
Posts: n/a
 
      1st Apr 2007
Great stuff Greg. Far more elegant than my attempts.
Protected was not reset if .Count > 1.
Fixed that but removed the test in the end because I have some merged cells.

Thanks for your solution,

Redleg

"Greg Wilson" wrote:

> I suggest you substitute the following. The problem with the previous version
> is that it will protect the wks even if it wasn't protected to begin with.
> This one will only protect it if it was already protected. Otherwise it could
> be a nuisance depending on what you are doing. The same comments hold
> regarding the password.
>
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> Dim protected As Boolean
> protected = Me.ProtectContents
> If protected Then Me.Unprotect ' "mypassword"
> Cells.FormatConditions.Delete
> If Len(copyval) > 0 Then CopyToClip (copyval)
> With Target
> If .Count > 1 Then Exit Sub
> copyval = .Value
> .FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
> .FormatConditions(1).Interior.ColorIndex = 19
> End With
> If protected Then Me.Protect ' "mypassword"
> End Sub
>
> Greg
>
> "Redleg" wrote:
>
> > Thanks for the help so far Greg.
> >
> > In both options I get a runtime error "Unable to set the ColorIndex property
> > of the interior class"
> >
> > "Greg Wilson" wrote:
> >
> > > This version appears to adequately compensate for the problem described if
> > > you need to only copy and paste values. You will need to set a reference to
> > > the Microsoft Forms 2.0 Object Library for it to work. Paste all of the below
> > > to the worksheet's code module. Minimal testing and never used personally:-
> > >
> > > Dim copyval As String
> > >
> > > Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> > > Cells.FormatConditions.Delete
> > > If Len(copyval) > 0 Then CopyToClip (copyval)
> > > With Target
> > > If .Count > 1 Then Exit Sub
> > > copyval = .Value
> > > .FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
> > > .FormatConditions(1).Interior.ColorIndex = 19
> > > End With
> > > End Sub
> > >
> > > Private Sub CopyToClip(txt)
> > > Dim DataObj As DataObject
> > > Set DataObj = New DataObject
> > > DataObj.SetText txt
> > > DataObj.PutInClipboard
> > > Set DataObj = Nothing
> > > End Sub
> > >
> > > Regards,
> > > Greg
> > >
> > >
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
selected cells doesnot change colour =?Utf-8?B?WmFycmFyIEphbmp1YQ==?= Microsoft Excel New Users 9 22nd May 2007 02:45 PM
RE: Change background colour of selected cells =?Utf-8?B?R3JlZyBXaWxzb24=?= Microsoft Excel Programming 0 28th Mar 2007 12:56 AM
RE: Change background colour of selected cells =?Utf-8?B?R3JlZyBXaWxzb24=?= Microsoft Excel Programming 0 28th Mar 2007 12:55 AM
change highlight colour of selected cells =?Utf-8?B?U3RhcnNreXMgQmlyZA==?= Microsoft Excel Misc 1 29th Apr 2006 02:27 PM
Change the background colour of form depending on option selected? =?Utf-8?B?UmljaGFyZCBIb3JuZQ==?= Microsoft Access 2 11th Feb 2005 03:33 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:11 AM.