number format

  • Thread starter Thread starter LiSa
  • Start date Start date
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?
 
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)
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top