the formatnumber() function does not display decimal numbers that have more than 3 decimal places

gogi100

New Member
Joined
Aug 9, 2013
Messages
26
i have next code
VBA Code:
Dim z As Range
Dim inputstring As String
Dim brojdecimalnihmesta As Integer
Dim DecSep As String
Dim ThousendSep As String
Dim CurrentSelectionRng As String
Dim eIndex As Long
Dim eRowIndex As Long


CurrentSelectionRng = Selection.Address
On Error Resume Next

MsgBox "Pre nego sto unesete format kolone: broj, tekst, datum, procenat; izaberite jednu celiju u koloni, koja ce biti selektovana", vbOKOnly
inputstring = InputBox(Prompt:="Unesi format kolone('b' - broj, 't' - tekst, 'd' - datum, 'p' - procenat", Title:="Format kolone")


 If inputstring = "b" Then
     MsgBox "Format celije(a) ce biti broj", vbOKOnly
    brojdecimalnihmesta = InputBox(Prompt:="Unesi broj decimalnih mesta", Title:="Decimalna mesta")
    eIndex = Application.ActiveCell.Column
    eRowIndex = Application.ActiveSheet.Cells(Rows.count, eIndex).End(xlUp).Row
    Range(Cells(2, eIndex), Cells(eRowIndex, eIndex)).Select
    System = Application.UseSystemSeparators
     With Application
        .DecimalSeparator = ","
        .ThousandsSeparator = "."
        .UseSystemSeparators = False
    End With
      If System = True Then
            DecSep = Application.International(xlDecimalSeparator)
            ThousendSep = Application.International(xlThousandsSeparator) 'not used;for information only
      Else
            DecSep = Application.DecimalSeparator
            ThousendSep = Application.ThousandsSeparator 'not used;for information only
      End If
 
  
   For Each z In Selection
  
  If brojdecimalnihmesta = 0 Then
        z.Value = FormatNumber(z.Value, brojdecimalnihmesta, , , vbTrue)
    ElseIf brojdecimalnihmesta = 1 Then
        z.Value = FormatNumber(z.Value, brojdecimalnihmesta, , , vbTrue)
    ElseIf brojdecimalnihmesta = 2 Then
        z.Value = FormatNumber(z.Value, brojdecimalnihmesta, , , vbTrue)
    ElseIf brojdecimalnihmesta = 3 Then
        z.Value = FormatNumber(z.Value, brojdecimalnihmesta, , , vbTrue)
    ElseIf brojdecimalnihmesta = 4 Then
        z.Value = FormatNumber(z.Value, brojdecimalnihmesta, , , vbTrue)
    ElseIf brojdecimalnihmesta = 5 Then
        z.Value = FormatNumber(z.Value, brojdecimalnihmesta, , , vbTrue)
    End If
  
   Next
    With Selection
  
    .HorizontalAlignment = xlRight
   ' .NumberFormat = "###,###,###,###0.00"
    
   End With
  
   Exit Sub
 End If

when I apply my macro each number for example 34563,9843 is displayed in the format 34,563.98 when I enter the number of decimal places 3 or 4 or 5. when I select the number of decimal places 1 the display in excel is 34,563.90. the display after the separator and decimals is fine with the menu, but why doesn't it display decimal places larger than 2.
I have the same problem with the percentage format.
I noticed when I entered the number of decimal places. This number actually rounds to that number of decimal places, and a always shows 2 decimal places
 
I need a screenshot of what it looks like on the other machine and also what you want it to look like.

By the way the code you have below, I would consider a very nasty piece of code.
You are changing the settings on someone's machine without telling them and withou resetting it when you finish.

VBA Code:
      If System = True Then
       With Application
        .DecimalSeparator = ","
        .ThousandsSeparator = "."
        .UseSystemSeparators = False
       End With
      End If
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
On my computer
1652114360513.png


this is what i want. the format is number in column, but on another computer i receive the same display, but format is problem i receive in corner error message 'this cell formated as text...'
 
Upvote 0
one more question, why on my computer this error does not displays but another computer this error is displays?
 
Upvote 0
I am unable to replicate the issue you have on the other machine.
Is the other machine using the same spreadsheet ?
If its a different spreadsheet perhaps share the spreadsheet with us using one of the sharing platforms eg dropbox, google drive etc

If it is the same spreadsheet, I don't know what it could be. You can try to send me a picture of what it looks like on the other machine to see if that helps me at all.
Also the exact data and input box responses you are using.
 
Upvote 0
I am unable to replicate the issue you have on the other machine.
Is the other machine using the same spreadsheet ?
If its a different spreadsheet perhaps share the spreadsheet with us using one of the sharing platforms eg dropbox, google drive etc

If it is the same spreadsheet, I don't know what it could be. You can try to send me a picture of what it looks like on the other machine to see if that helps me at all.
Also the exact data and input box responses you are using.
thanks for help, i resolved my problem with code above
 
Upvote 0
OK sounds good.
PS: Please get rid of those lines that deactivate the "Use systems separators", I believe you don't need it for your code to work and will cause anyone who runs it a great deal of frustration when it mucks up their Excel.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top