Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Conditional Formatting - number of decimal places

I wish to change the number of decimal places displayed in a range of cells,
dependant on the contents of a specific cell.

A1 2
A2
A3 1.23 } these cells formatted to 2dp
A4 2.34 } because A1 contains 2.
A5 3.45 }
A6
A7 7.02 } formula =SUM(A3:A5)

If I wanted to set the font, border or pattern of the cell I could use
conditional formatting. However, conditional formatting cannot control the
number of decimal; places displayed.

The values in cells A3:A5 are always the result of a formula (not entered
directly) so I've tried writing a macro function that uses Format( ) to
format a number the way I want. However, this then returns the value as a
string, which means that I can't use the displayed results in a sum
function.

Is there an obvious solution to this?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Conditional Formatting - number of decimal places

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Range("A2:A20").NumberFormat = "0." & String(Target.Value, "0")
End If
End Sub


Right click on the sheet tab and select View Code

paste in the above:

--
Regards,
Tom Ogilvy


"Andrew" wrote in message
...
I wish to change the number of decimal places displayed in a range of

cells,
dependant on the contents of a specific cell.

A1 2
A2
A3 1.23 } these cells formatted to 2dp
A4 2.34 } because A1 contains 2.
A5 3.45 }
A6
A7 7.02 } formula =SUM(A3:A5)

If I wanted to set the font, border or pattern of the cell I could use
conditional formatting. However, conditional formatting cannot control

the
number of decimal; places displayed.

The values in cells A3:A5 are always the result of a formula (not entered
directly) so I've tried writing a macro function that uses Format( ) to
format a number the way I want. However, this then returns the value as a
string, which means that I can't use the displayed results in a sum
function.

Is there an obvious solution to this?




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Conditional Formatting - number of decimal places

Tom Ogilvy wrote:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
Range("A2:A20").NumberFormat = "0." & String(Target.Value, "0")
End If
End Sub


Right click on the sheet tab and select View Code

paste in the above:


Thanks. I had sort of homed in on this idea by looking at
http://www.cpearson.com/excel/events.htm
As a test I wrote:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = "C1" Then
Application.EnableEvents = False
Range("C3").Value = "Changed"
Application.EnableEvents = True
End If
End Sub

If I apply this to anew worksheet in the existing workbook it works. If I
apply it to my existing worksheet it doesn't. I'm not too hot on debugging
VB functions, but if I introduce a syntax error into the code (a rather
crude debugging method!), an error message ccurs when I change cell C1, so
the event is clearly being detected, but nothing is written into cell C3
even with correct(?) code.

Any clues as to where I might be going wrong (or probably more likely, how
do I debug the code?)


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
Chart Number Decimal Places John Excel Discussion (Misc queries) 6 July 2nd 09 08:17 PM
Conditional formatting & decimal part of a number in cell Mac Excel Worksheet Functions 4 August 16th 08 10:45 PM
conditonally formatting decimal places BigMikeGallagher Excel Discussion (Misc queries) 0 April 6th 06 06:08 PM
Setting the number of decimal places for a text box. Aaron1978 Excel Discussion (Misc queries) 3 March 8th 06 04:59 PM
Converting a number to 2 decimal places mattslav Excel Discussion (Misc queries) 2 June 4th 05 04:10 AM


All times are GMT +1. The time now is 10:05 PM.

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

About Us

"It's about Microsoft Excel"