ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Custom Zeros (https://www.excelbanter.com/excel-worksheet-functions/260565-custom-zeros.html)

Totoshi

Custom Zeros
 
Hi,


i have a issue in excel 2003 VBA, how can i costumize the decimal
places for a range of cells with the number format from other cell.
For example :

in Column A i have 1.21 ; 1.56 ; 1.789 ; 1.9899 ; 0.1

and in cell B1 i have the number 0.001 .

So i want to format by a function Column A so the contents in cell
appears like this :

1.210 ; 1.560 ; 1.789 ; 1.990 ; 0,100 .

Can anyone help me with this ?

Thank you , Cheers

Bob Phillips[_4_]

Custom Zeros
 
Try

Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "B1" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target

If IsNumeric(.Value2) Then

Me.Columns(1).NumberFormat = "#,##0." & Left$("0000000000",
Len(.Value2) - InStr(.Value2, "."))
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

"Totoshi" wrote in message
...
Hi,


i have a issue in excel 2003 VBA, how can i costumize the decimal
places for a range of cells with the number format from other cell.
For example :

in Column A i have 1.21 ; 1.56 ; 1.789 ; 1.9899 ; 0.1

and in cell B1 i have the number 0.001 .

So i want to format by a function Column A so the contents in cell
appears like this :

1.210 ; 1.560 ; 1.789 ; 1.990 ; 0,100 .

Can anyone help me with this ?

Thank you , Cheers




Mike H

Custom Zeros
 
Hi,

Try this

Private Sub Worksheet_Change(ByVal Target As Range)
Dim MyRange As Range
Dim LastRow As Long
If Target.Cells.Count 1 Or IsEmpty(Target) Then Exit Sub
If Target.Address = "$B$1" Then
Application.EnableEvents = False
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Set MyRange = Range("A1:A" & LastRow)
r = Len(Range("B1") - Int(Range("B1"))) - 2
MyRange.NumberFormat = "0." & WorksheetFunction.Rept("0", r)
End If
Application.EnableEvents = True
End Sub

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Totoshi" wrote:

Hi,


i have a issue in excel 2003 VBA, how can i costumize the decimal
places for a range of cells with the number format from other cell.
For example :

in Column A i have 1.21 ; 1.56 ; 1.789 ; 1.9899 ; 0.1

and in cell B1 i have the number 0.001 .

So i want to format by a function Column A so the contents in cell
appears like this :

1.210 ; 1.560 ; 1.789 ; 1.990 ; 0,100 .

Can anyone help me with this ?

Thank you , Cheers
.



All times are GMT +1. The time now is 04:46 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com