imputbox change decimal value dot to comma

Blue_Wings

New Member
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
How about
Code:
    If InStr(1, resposta, ".") > 0 Then resposta = Replace(resposta, ".", ",")
 
Last edited:

Blue_Wings

New Member
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
Change this
Code:
ValorND = [COLOR=#ff0000]Val[/COLOR](ValorND)
Otherwise you're converting it to an integer
 

Blue_Wings

New Member
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 .
 

Blue_Wings

New Member
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
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
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
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
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top