When i am running the following code below (only a part of the full code), and for instant writing the value "0,9", then the inputbox returns "89,9999976158142%" in excel.
Further, when i later in the code has another where the user is supposed to choose between "Pipeline, Ordre, tilbud, afsluttet and tabt", then in excel it returns the same value as where written for the procent, "89,9999976158142%", instead of some of the options??**
Procent = InputBox("Indtast procent : (0 - 0,10 -0,25 - 0,50 - 0,75 - 0,90 - 1", "procent")
If Procent = "0" Or Procent = "0,10" Or Procent = "0,25" Or Procent = "0,5" Or Procent = "0,75" Or Procent = "0,9" Or Procent = "1" Then
GoTo videre2
Else
Videre5:
MsgBox "mistake"
Procent = InputBox("Indtast Procent : (0 - 0,10 -0,25 - 0,50 - 0,75 - 0,90 - 1", "Procent ")
If Procent = "0" Or Procent = "0,10" Or Procent = "0,25" Or Procent = "0,5" Or Procent = "0,75" Or Procent = "0,9" Or Procent = "1" Then
GoTo videre2
Else: GoTo Videre5
End If
End If
videre2:
Cells(Lastrow, "E").Value = Procent
Kategori = InputBox("Indtast Kategori: Pipeline - Ordre - Tilbud - Afsluttet - Tabt", "Kategori")
'Cells(Lastrow, "I").Value = Kategori
If Kategori = "Pipeline" Or Kategori = "Ordre" Or Kategori = "Tilbud" Or Kategori = "Afsluttet" Or Kategori = "Tabt" Then
GoTo videre3
Else
Videre6:
MsgBox "Stavefejl"
Kategori = InputBox("Indtast Kategori: Pipeline - Ordre - Tilbud - Afsluttet - Tabt", "Kategori")
If Kategori = "Pipeline" Or Kategori = "Ordre" Or Kategori = "Tilbud" Or Kategori = "Afsluttet" Or Kategori = "Tabt" Then
GoTo videre3
Else: GoTo Videre6
End If
End If
videre3:
Cells(Lastrow, "I").Value = Sandsynlighed
I can not find the mistake??
CodePudding user response:
Unfortunately, you don't provide some important informations.
a) If you enter 0.9 into a cell that is formatted as Percentage, it is shown as 90%, so I assume that your cell is formatted as Percentage. If you really want 0.9%, divide the value by 100 before writing it to the cell.
b) Obviously, you have an rounding problem. You don't show how the variable Procent is declared, but I assume that you have declared it as Single. This is bad for 2 reasons: You will get rounding errors, and you will get a runtime error if something non-numeric is entered. Declare the variable as String or Variant and after checking for validity, convert it to Double (not to Single).
c) According to the code you show, you write the second answer into variable Kategori, but you assign the variable Sandsynlighed to the cell.
d) Your code could be improved. Avoid GoTo and avoid to duplicate the input and check statements. One idea could be:
Do While True
Dim Procent As Variant
Procent = InputBox("Indtast procent : (0 - 0,10 -0,25 - 0,50 - 0,75 - 0,90 - 1", "procent")
If Procent = "0" Or Procent = "0,10" Or Procent = "0,25" Or Procent = "0,5" Or Procent = "0,75" Or Procent = "0,9" Or Procent = "1" Then Exit Do
Loop
Cells(lastRow, "E").Value = CDbl(Procent) ' (or maybe CDbl(Procent) / 100)
CodePudding user response:
Have in mind, that an InputBox always returns text. Thus, this must be converted to numeric for a test, and - as you use comma as the decimal separator - the Cxxx functions must be used.
Also, what you see it floating point errors, which you can avoid by using Decimal or Currency. So, follow a route like this:
Public Function CheckPercent()
Const Prompt As String = "Indtast procent: 0 - 0,10 - 0,25 - 0,50 - 0,75 - 0,90 - 1"
Const Title As String = "Procent"
Dim Text As String
Dim Procent As Currency
Dim Continue As Boolean
Text = InputBox(Prompt, Title)
If IsNumeric(Text) Then
Procent = CCur(Text)
Select Case Procent
Case 0, 0.1, 0.25, 0.5, 0.75, 0.9, 1
Continue = True
End Select
End If
If Continue = False Then
' Try again - or other option
End If
Debug.Print "Continue:", Continue
' <snip>
End Function
