當我在下面運行以下代碼(僅是完整代碼的一部分)并立即寫入值“0,9”時,輸入框在 excel 中返??回“89,9999976158142%”。
此外,當我稍后在代碼中有另一個用戶應該在“管道、Ordre、tilbud、afsluttet 和 tabt”之間進行選擇時,然后在 excel 中它回傳與為 procent 撰寫的值相同的值,“89,9999976158142% ",而不是某些選項??**
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
我找不到錯誤??
uj5u.com熱心網友回復:
不幸的是,您沒有提供一些重要資訊。
a)如果你進入到0.9被格式化為百分比細胞時,它顯示為90%,所以我認為你的細胞被格式化為百分比。如果您真的想要 0.9%,請在將其寫入單元格之前將該值除以 100。
b) 顯然,你有一個舍入問題。您沒有顯示變數Procent是如何宣告的,但我假設您已將其宣告為Single. 這有兩個原因:你會得到舍入錯誤,如果輸入了非數字的東西,你會得到運行時錯誤。將變數宣告為 String 或 Variant,并在檢查有效性后將其轉換為 Double(而不是 Single)。
c)根據您顯示的代碼,您將第二個答案寫入 variable Kategori,但您將變數分配Sandsynlighed給單元格。
d)您的代碼可以改進。避免GoTo和避免重復輸入和檢查陳述句。一個想法可能是:
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)
uj5u.com熱心網友回復:
請記住,InputBox總是回傳文本。因此,必須將其轉換為數字以進行測驗,并且 - 當您使用逗號作為小數分隔符時- 必須使用 Cxxx 函式。
此外,您看到的浮點錯誤,可以通過使用Decimal或Currency來避免。所以,遵循這樣的路線:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407575.html
標籤:
