imputbox change decimal value dot to comma

Blue_Wings

New Member
Joined
Aug 23, 2019
Messages
11
Hi, I have been searching without success for the following:

I have a imputbox to receive euros, if the user wants to insert for example the number 55.20, afterwords I will use that number to do another calculation, but is giving me an error because its inserted with a dot as a decimal separator instead of a comma, if I use a dot separator gives me the following error:

Run-time error'13': Type mismatch



Can I use something like: resposta = Replace(Textnumber.Value, ".", ",") to force the change from dot to comma to avoid the system error?



Example of the code:

Code:
    Dim resposta As Variant
    
    
    resposta = InputBox("Valor da Nota de Débito")
    
    
    Dim ValorQuota As Variant
    ValorQuota = WorksheetFunction.Sum(Range("H4:H5"))

    
    Dim Resultado As Variant
    
        
    Resultado = resposta / ValorQuota
    
    MsgBox Resultado

Thank you.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How about
Code:
    If InStr(1, resposta, ".") > 0 Then resposta = Replace(resposta, ".", ",")
 
Last edited:
Upvote 0
It works :) but then is resulting in another problem.

after the calculation is being stored as string in "ValoND" and I was trying to convert it to number but keeping de decimal part, for example the number 55,70 in the inputbox is returned as 56,00 by ValorND in cell "O5", do you have any ideas to keep a decimal number in the same way 55,70? Thank you.
I had changed some names of the variables for better understanding, sorry.

Example:

Code:
    'Imput box para pedir valor de Nota de Débito
    
    Dim ValorND As String
    Dim ValorQuota As String
    Dim Resultado As String
    
    ValorND = InputBox("Valor da Nota de Débito")
        
    'Se for inserido um "ponto" transforma em virgula
    If InStr(1, ValorND, ".") > 0 Then ValorND = Replace(ValorND, ".", ",")
    
    ValorND = CInt(ValorND)
    ValorND = FormatNumber(ValorND, 2)

    
    'Exit Sub
   
   
    'Soma a quota com o fundo de reserva para achar o valor total da quota da fração em questão
    
    ValorQuota = WorksheetFunction.Sum(Range("H4:H5"))

    
 
    
    'Faz o teste para ver se é numero inteiro ou com casas decimais
    
    Resultado = ValorND / ValorQuota
    
    MsgBox Resultado
    
    Range("O5").Value = ValorND
    
    Exit Sub
 
Upvote 0
Change this
Code:
ValorND = [COLOR=#ff0000]Val[/COLOR](ValorND)
Otherwise you're converting it to an integer
 
Upvote 0
Change this
Code:
ValorND = [COLOR=#ff0000]Val[/COLOR](ValorND)
Otherwise you're converting it to an integer

Even with that change,

If I put in the inputbox for example 55.70 the output from the variable ValorND in the cell "O5" is 55.00 and I wanted to be 55.70

Rich (BB code):
ValorND = Val(ValorND)

'I added this line to make sure the decimal part was there but the value continues rounded to 55 
ValorND = FormatNumber(ValorND, 2)

I cannot understand what is happening to de decimal part, simply disappears .
 
Upvote 0
Is your system set to use a . or a , for decimals?
 
Upvote 0
The system uses , for decimals.

Sorry for all the trouble, all that I wanted was an simple input box to store a value in euros, that most of the times have decimal part, keep it in a variable, do some calculation and paste it in a cell as a number with that same decimal part.
 
Last edited:
Upvote 0
Ok, that sounds as though VBA requires a . for decimals regardless of local settings.
Try
Code:
    'Imput box para pedir valor de Nota de Débito
    
    Dim ValorND As String
    Dim ValorQuota As String
    Dim Resultado As String
    
    ValorND = InputBox("Valor da Nota de Débito")
        
    'Se for inserido um "ponto" transforma em virgula
   
    'Soma a quota com o fundo de reserva para achar o valor total da quota da fração em questão
    
    ValorQuota = WorksheetFunction.Sum(Range("H4:H5"))

    
 
    
    'Faz o teste para ver se é numero inteiro ou com casas decimais
    
    Resultado = ValorND / ValorQuota
    
    MsgBox Resultado
    If InStr(1, ValorND, ".") > 0 Then ValorND = Replace(ValorND, ".", ",")

    Range("O5").Value = ValorND
    
    Exit Sub
 
Upvote 0
This last option keeps the decimal part like it should be but, for example in the inputbox 55.7 it stores in the cell 55,7 but as text and not as a number.

I need it to be a number in the spreadsheet to be able to sum
 
Last edited:
Upvote 0
Try this:
Format cell O5 as Number with 2 decimal places, then run this code:

Code:
Sub a1107817a()
   'Imput box para pedir valor de Nota de Débito
    
    Dim ValorND
    Dim ValorQuota
    Dim Resultado
    
    ValorND = InputBox("Valor da Nota de Débito")
        
    'Se for inserido um "ponto" transforma em virgula
   
    'Soma a quota com o fundo de reserva para achar o valor total da quota da fração em questão
    If InStr(1, ValorND, ",") > 0 Then ValorND = Replace(ValorND, ",", ".")
    ValorND = Val(ValorND)
    ValorQuota = WorksheetFunction.Sum(Range("H4:H5"))

    
 
    
    'Faz o teste para ver se é numero inteiro ou com casas decimais
    
    Resultado = ValorND / ValorQuota
    
'    MsgBox Resultado

    Range("O5").Value = ValorND
    

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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