number format

L

LiSa

I'd like a person to be able to alter the number of
decimal places in one cell by typing a number in another.

eg if you type 3 in A1 then A2 is formatted to 3 decimal
places.
I've recored a macro to see the code but the number format
is "0.000"

Is there any way of doing this?
 
B

Bob Phillips

Lisa,

This works for A3. Change the range if you want to apply to more
Dim sFormat As String

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A3")) Is Nothing Then
With Target
If IsNumeric(.Value) Then
sFormat = "0." & Left("0000000000", .Value)
.Offset(-1, 0).NumberFormat = sFormat
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
S

Stan Scott

Use ROUND, referring to A1. This formula rounds the value in cell D1 to the
number of decimal places in A1:

=ROUND(D1,A1)

Stan Scott
NYC
 
Top