PC Review


Reply
Thread Tools Rate Thread

Conditional format : return to default format

 
 
=?Utf-8?B?YW5keQ==?=
Guest
Posts: n/a
 
      27th Jun 2007
hello,

the following code allows me to add more than 3 conditional formats to
fields in the range containing numbers -1, -0.5, 0.5 or 1.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
If rng Is Nothing Then
Exit Sub
Else
Dim cl As Range
For Each cl In rng
Select Case cl.Text
Case 1
cl.Interior.ColorIndex = 11
cl.Font.ColorIndex = 11
Case 0.5
cl.Interior.ColorIndex = 41
cl.Font.ColorIndex = 41
Case -1
cl.Interior.ColorIndex = 16
cl.Font.ColorIndex = 16
Case -0.5
cl.Interior.ColorIndex = 15
cl.Font.ColorIndex = 15
Case Else
Exit Sub
End Select
Next cl
End If
End Sub

i have two problems:
- when i delete the values, i would like excel to turn to default formatting
again automatically (i.e. no background and black color text).
- how can i apply the code to values that are already in the range (have
excel update the range automatically when i open the workbook) ?

as you notice, i'm new to VBA...

thanks.
andy
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlrZSBI?=
Guest
Posts: n/a
 
      27th Jun 2007
Try this:-

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
If rng Is Nothing Then
Exit Sub
Else
Dim cl As Range
For Each cl In rng
Select Case cl.Text
Case 1
cl.Interior.ColorIndex = 11
cl.Font.ColorIndex = 11
Case 0.5
cl.Interior.ColorIndex = 41
cl.Font.ColorIndex = 41
Case -1
cl.Interior.ColorIndex = 16
cl.Font.ColorIndex = 16
Case -0.5
cl.Interior.ColorIndex = 15
cl.Font.ColorIndex = 15
Case Else
cl.Interior.ColorIndex = xlNone
cl.Font.ColorIndex = 0
Exit Sub
End Select
Next cl
End If
End Sub

Mike

"andy" wrote:

> hello,
>
> the following code allows me to add more than 3 conditional formats to
> fields in the range containing numbers -1, -0.5, 0.5 or 1.
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> Dim rng As Range
> Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
> If rng Is Nothing Then
> Exit Sub
> Else
> Dim cl As Range
> For Each cl In rng
> Select Case cl.Text
> Case 1
> cl.Interior.ColorIndex = 11
> cl.Font.ColorIndex = 11
> Case 0.5
> cl.Interior.ColorIndex = 41
> cl.Font.ColorIndex = 41
> Case -1
> cl.Interior.ColorIndex = 16
> cl.Font.ColorIndex = 16
> Case -0.5
> cl.Interior.ColorIndex = 15
> cl.Font.ColorIndex = 15
> Case Else
> Exit Sub
> End Select
> Next cl
> End If
> End Sub
>
> i have two problems:
> - when i delete the values, i would like excel to turn to default formatting
> again automatically (i.e. no background and black color text).
> - how can i apply the code to values that are already in the range (have
> excel update the range automatically when i open the workbook) ?
>
> as you notice, i'm new to VBA...
>
> thanks.
> andy

 
Reply With Quote
 
=?Utf-8?B?YW5keQ==?=
Guest
Posts: n/a
 
      27th Jun 2007
hello Mike,

thanks, works just fine !
when i delete three values at a time, though, only the format of the first
cell selected returns to default. can default format can be applied to all
the selected fields?

thanks again.
andy

"Mike H" wrote:

> Try this:-
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> Dim rng As Range
> Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
> If rng Is Nothing Then
> Exit Sub
> Else
> Dim cl As Range
> For Each cl In rng
> Select Case cl.Text
> Case 1
> cl.Interior.ColorIndex = 11
> cl.Font.ColorIndex = 11
> Case 0.5
> cl.Interior.ColorIndex = 41
> cl.Font.ColorIndex = 41
> Case -1
> cl.Interior.ColorIndex = 16
> cl.Font.ColorIndex = 16
> Case -0.5
> cl.Interior.ColorIndex = 15
> cl.Font.ColorIndex = 15
> Case Else
> cl.Interior.ColorIndex = xlNone
> cl.Font.ColorIndex = 0
> Exit Sub
> End Select
> Next cl
> End If
> End Sub
>
> Mike
>
> "andy" wrote:
>
> > hello,
> >
> > the following code allows me to add more than 3 conditional formats to
> > fields in the range containing numbers -1, -0.5, 0.5 or 1.
> >
> > Private Sub Worksheet_Change(ByVal Target As Range)
> > Dim rng As Range
> > Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
> > If rng Is Nothing Then
> > Exit Sub
> > Else
> > Dim cl As Range
> > For Each cl In rng
> > Select Case cl.Text
> > Case 1
> > cl.Interior.ColorIndex = 11
> > cl.Font.ColorIndex = 11
> > Case 0.5
> > cl.Interior.ColorIndex = 41
> > cl.Font.ColorIndex = 41
> > Case -1
> > cl.Interior.ColorIndex = 16
> > cl.Font.ColorIndex = 16
> > Case -0.5
> > cl.Interior.ColorIndex = 15
> > cl.Font.ColorIndex = 15
> > Case Else
> > Exit Sub
> > End Select
> > Next cl
> > End If
> > End Sub
> >
> > i have two problems:
> > - when i delete the values, i would like excel to turn to default formatting
> > again automatically (i.e. no background and black color text).
> > - how can i apply the code to values that are already in the range (have
> > excel update the range automatically when i open the workbook) ?
> >
> > as you notice, i'm new to VBA...
> >
> > thanks.
> > andy

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBI?=
Guest
Posts: n/a
 
      27th Jun 2007
Andy,

That should do it-
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
If rng Is Nothing Then
Exit Sub
Else
Dim cl As Range
For Each cl In rng
Select Case cl.Text
Case 1
cl.Interior.ColorIndex = 11
cl.Font.ColorIndex = 11
Case 0.5
cl.Interior.ColorIndex = 41
cl.Font.ColorIndex = 41
Case -1
cl.Interior.ColorIndex = 16
cl.Font.ColorIndex = 16
Case -0.5
cl.Interior.ColorIndex = 15
cl.Font.ColorIndex = 15
Case Else
Selection.Interior.ColorIndex = xlNone
cl.Font.ColorIndex = 0
Exit Sub
End Select
Next cl
End If
End Sub


Mike


"andy" wrote:

> hello Mike,
>
> thanks, works just fine !
> when i delete three values at a time, though, only the format of the first
> cell selected returns to default. can default format can be applied to all
> the selected fields?
>
> thanks again.
> andy
>
> "Mike H" wrote:
>
> > Try this:-
> >
> > Private Sub Worksheet_Change(ByVal Target As Range)
> > Dim rng As Range
> > Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
> > If rng Is Nothing Then
> > Exit Sub
> > Else
> > Dim cl As Range
> > For Each cl In rng
> > Select Case cl.Text
> > Case 1
> > cl.Interior.ColorIndex = 11
> > cl.Font.ColorIndex = 11
> > Case 0.5
> > cl.Interior.ColorIndex = 41
> > cl.Font.ColorIndex = 41
> > Case -1
> > cl.Interior.ColorIndex = 16
> > cl.Font.ColorIndex = 16
> > Case -0.5
> > cl.Interior.ColorIndex = 15
> > cl.Font.ColorIndex = 15
> > Case Else
> > cl.Interior.ColorIndex = xlNone
> > cl.Font.ColorIndex = 0
> > Exit Sub
> > End Select
> > Next cl
> > End If
> > End Sub
> >
> > Mike
> >
> > "andy" wrote:
> >
> > > hello,
> > >
> > > the following code allows me to add more than 3 conditional formats to
> > > fields in the range containing numbers -1, -0.5, 0.5 or 1.
> > >
> > > Private Sub Worksheet_Change(ByVal Target As Range)
> > > Dim rng As Range
> > > Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
> > > If rng Is Nothing Then
> > > Exit Sub
> > > Else
> > > Dim cl As Range
> > > For Each cl In rng
> > > Select Case cl.Text
> > > Case 1
> > > cl.Interior.ColorIndex = 11
> > > cl.Font.ColorIndex = 11
> > > Case 0.5
> > > cl.Interior.ColorIndex = 41
> > > cl.Font.ColorIndex = 41
> > > Case -1
> > > cl.Interior.ColorIndex = 16
> > > cl.Font.ColorIndex = 16
> > > Case -0.5
> > > cl.Interior.ColorIndex = 15
> > > cl.Font.ColorIndex = 15
> > > Case Else
> > > Exit Sub
> > > End Select
> > > Next cl
> > > End If
> > > End Sub
> > >
> > > i have two problems:
> > > - when i delete the values, i would like excel to turn to default formatting
> > > again automatically (i.e. no background and black color text).
> > > - how can i apply the code to values that are already in the range (have
> > > excel update the range automatically when i open the workbook) ?
> > >
> > > as you notice, i'm new to VBA...
> > >
> > > thanks.
> > > andy

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBI?=
Guest
Posts: n/a
 
      27th Jun 2007
Andy,

Missed a line. Change both these lines:-

Case Else
cl.Interior.ColorIndex = xlNone
cl.Font.ColorIndex = 0

to

Case Else
selection.Interior.ColorIndex = xlNone
selection.Font.ColorIndex = 0

Mike

"andy" wrote:

> hello Mike,
>
> thanks, works just fine !
> when i delete three values at a time, though, only the format of the first
> cell selected returns to default. can default format can be applied to all
> the selected fields?
>
> thanks again.
> andy
>
> "Mike H" wrote:
>
> > Try this:-
> >
> > Private Sub Worksheet_Change(ByVal Target As Range)
> > Dim rng As Range
> > Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
> > If rng Is Nothing Then
> > Exit Sub
> > Else
> > Dim cl As Range
> > For Each cl In rng
> > Select Case cl.Text
> > Case 1
> > cl.Interior.ColorIndex = 11
> > cl.Font.ColorIndex = 11
> > Case 0.5
> > cl.Interior.ColorIndex = 41
> > cl.Font.ColorIndex = 41
> > Case -1
> > cl.Interior.ColorIndex = 16
> > cl.Font.ColorIndex = 16
> > Case -0.5
> > cl.Interior.ColorIndex = 15
> > cl.Font.ColorIndex = 15
> > Case Else
> > cl.Interior.ColorIndex = xlNone
> > cl.Font.ColorIndex = 0
> > Exit Sub
> > End Select
> > Next cl
> > End If
> > End Sub
> >
> > Mike
> >
> > "andy" wrote:
> >
> > > hello,
> > >
> > > the following code allows me to add more than 3 conditional formats to
> > > fields in the range containing numbers -1, -0.5, 0.5 or 1.
> > >
> > > Private Sub Worksheet_Change(ByVal Target As Range)
> > > Dim rng As Range
> > > Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
> > > If rng Is Nothing Then
> > > Exit Sub
> > > Else
> > > Dim cl As Range
> > > For Each cl In rng
> > > Select Case cl.Text
> > > Case 1
> > > cl.Interior.ColorIndex = 11
> > > cl.Font.ColorIndex = 11
> > > Case 0.5
> > > cl.Interior.ColorIndex = 41
> > > cl.Font.ColorIndex = 41
> > > Case -1
> > > cl.Interior.ColorIndex = 16
> > > cl.Font.ColorIndex = 16
> > > Case -0.5
> > > cl.Interior.ColorIndex = 15
> > > cl.Font.ColorIndex = 15
> > > Case Else
> > > Exit Sub
> > > End Select
> > > Next cl
> > > End If
> > > End Sub
> > >
> > > i have two problems:
> > > - when i delete the values, i would like excel to turn to default formatting
> > > again automatically (i.e. no background and black color text).
> > > - how can i apply the code to values that are already in the range (have
> > > excel update the range automatically when i open the workbook) ?
> > >
> > > as you notice, i'm new to VBA...
> > >
> > > thanks.
> > > andy

 
Reply With Quote
 
=?Utf-8?B?YW5keQ==?=
Guest
Posts: n/a
 
      27th Jun 2007
perfect ! thanks a lot.


"Mike H" wrote:

> Andy,
>
> Missed a line. Change both these lines:-
>
> Case Else
> cl.Interior.ColorIndex = xlNone
> cl.Font.ColorIndex = 0
>
> to
>
> Case Else
> selection.Interior.ColorIndex = xlNone
> selection.Font.ColorIndex = 0
>
> Mike
>
> "andy" wrote:
>
> > hello Mike,
> >
> > thanks, works just fine !
> > when i delete three values at a time, though, only the format of the first
> > cell selected returns to default. can default format can be applied to all
> > the selected fields?
> >
> > thanks again.
> > andy
> >
> > "Mike H" wrote:
> >
> > > Try this:-
> > >
> > > Private Sub Worksheet_Change(ByVal Target As Range)
> > > Dim rng As Range
> > > Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
> > > If rng Is Nothing Then
> > > Exit Sub
> > > Else
> > > Dim cl As Range
> > > For Each cl In rng
> > > Select Case cl.Text
> > > Case 1
> > > cl.Interior.ColorIndex = 11
> > > cl.Font.ColorIndex = 11
> > > Case 0.5
> > > cl.Interior.ColorIndex = 41
> > > cl.Font.ColorIndex = 41
> > > Case -1
> > > cl.Interior.ColorIndex = 16
> > > cl.Font.ColorIndex = 16
> > > Case -0.5
> > > cl.Interior.ColorIndex = 15
> > > cl.Font.ColorIndex = 15
> > > Case Else
> > > cl.Interior.ColorIndex = xlNone
> > > cl.Font.ColorIndex = 0
> > > Exit Sub
> > > End Select
> > > Next cl
> > > End If
> > > End Sub
> > >
> > > Mike
> > >
> > > "andy" wrote:
> > >
> > > > hello,
> > > >
> > > > the following code allows me to add more than 3 conditional formats to
> > > > fields in the range containing numbers -1, -0.5, 0.5 or 1.
> > > >
> > > > Private Sub Worksheet_Change(ByVal Target As Range)
> > > > Dim rng As Range
> > > > Set rng = Intersect(Target, Range("C24:AF54,C601:AF91,C97:AF127"))
> > > > If rng Is Nothing Then
> > > > Exit Sub
> > > > Else
> > > > Dim cl As Range
> > > > For Each cl In rng
> > > > Select Case cl.Text
> > > > Case 1
> > > > cl.Interior.ColorIndex = 11
> > > > cl.Font.ColorIndex = 11
> > > > Case 0.5
> > > > cl.Interior.ColorIndex = 41
> > > > cl.Font.ColorIndex = 41
> > > > Case -1
> > > > cl.Interior.ColorIndex = 16
> > > > cl.Font.ColorIndex = 16
> > > > Case -0.5
> > > > cl.Interior.ColorIndex = 15
> > > > cl.Font.ColorIndex = 15
> > > > Case Else
> > > > Exit Sub
> > > > End Select
> > > > Next cl
> > > > End If
> > > > End Sub
> > > >
> > > > i have two problems:
> > > > - when i delete the values, i would like excel to turn to default formatting
> > > > again automatically (i.e. no background and black color text).
> > > > - how can i apply the code to values that are already in the range (have
> > > > excel update the range automatically when i open the workbook) ?
> > > >
> > > > as you notice, i'm new to VBA...
> > > >
> > > > thanks.
> > > > andy

 
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
Conditional Format & Default Format Help Brian Microsoft Excel Misc 1 31st Jan 2008 10:37 PM
Re: WORD 97, how to change 3 col'm return label format to list format Doug Robbins - Word MVP Microsoft Word Document Management 0 4th Jan 2007 06:30 PM
How to return the default setting of numbering format in Excel? =?Utf-8?B?c2F0dXJu?= Microsoft Excel Misc 1 7th Jul 2006 07:40 AM
make a conditional format the default Fred Evans Microsoft Excel Misc 3 6th Dec 2004 05:01 AM
Keep format after paste from other worksheets - conditional format or EnableControl solution doesn't work Microsoft Excel Programming 0 3rd May 2004 12:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:29 AM.