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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,567
Office Version
365
Platform
Windows
How about
Code:
    If InStr(1, resposta, ".") > 0 Then resposta = Replace(resposta, ".", ",")
 
Last edited:

Blue_Wings

New Member
Joined
Aug 23, 2019
Messages
11
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,567
Office Version
365
Platform
Windows
Change this
Code:
ValorND = [COLOR=#ff0000]Val[/COLOR](ValorND)
Otherwise you're converting it to an integer
 

Blue_Wings

New Member
Joined
Aug 23, 2019
Messages
11
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 .
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,567
Office Version
365
Platform
Windows
Is your system set to use a . or a , for decimals?
 

Blue_Wings

New Member
Joined
Aug 23, 2019
Messages
11
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:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,567
Office Version
365
Platform
Windows
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
 

Blue_Wings

New Member
Joined
Aug 23, 2019
Messages
11
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:

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,266
Office Version
2013
Platform
Windows
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
 

Forum statistics

Threads
1,078,500
Messages
5,340,743
Members
399,393
Latest member
farlow

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top