Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA—将0,02传递给整数时,它';s值为0_Vba_Excel_Types_Decimal - Fatal编程技术网

VBA—将0,02传递给整数时,它';s值为0

VBA—将0,02传递给整数时,它';s值为0,vba,excel,types,decimal,Vba,Excel,Types,Decimal,此函数用于根据客户的收入确定促销百分比。这是一个缩放示例: Without Limit -> 2.0% from 30.000,- -> 3,0% from 50.000,- -> 3,5% from 75.000,- -> 4,0% 所有值都从Excel工作表中检索并存储在long中 问题是,当确定提升层时,我将百分比值传递给函数(数据类型为Integer) xSt3P为0.03,但安装百分比为0 以下是函数: Public Funct

此函数用于根据客户的收入确定促销百分比。这是一个缩放示例:

Without Limit     -> 2.0%
from 30.000,-     -> 3,0%
from 50.000,-     -> 3,5%
from 75.000,-     -> 4,0%
所有值都从Excel工作表中检索并存储在long中

问题是,当确定提升层时,我将百分比值传递给函数(数据类型为Integer)

xSt3P为0.03,但安装百分比为0

以下是函数:

Public Function PercentageInstallation(xRwCtr As Integer, xKdnr1 As Integer, xSource As Worksheet) As Integer

    Dim xUmsatz, xOLiP, xSt1B, xSt1P, xSt2B, xSt2P, xSt3B, xSt3P, xSt4B, xSt4P As Long

    xUmsatz = xSource.Cells(xRwCtr, "I")    'Gesamtumsatz
    xOLiP = xSource.Cells(xRwCtr, "Q")      'ohne Limit
    xSt1B = xSource.Cells(xRwCtr, "R")      'Stufe 1 Betrag
    xSt1P = xSource.Cells(xRwCtr, "S")      'Stufe 1 Prozent
    xSt2B = xSource.Cells(xRwCtr, "T")      'Stufe 2 Betrag
    xSt2P = xSource.Cells(xRwCtr, "U")      'Stufe 2 Prozent
    xSt3B = xSource.Cells(xRwCtr, "V")      'Stufe 3 Betrag
    xSt3P = xSource.Cells(xRwCtr, "W")      'Stufe 3 Prozent
    xSt4B = xSource.Cells(xRwCtr, "X")      'Stufe 4 Betrag
    xSt4P = xSource.Cells(xRwCtr, "Y")      'Stufe 4 Prozent

    Debug.Print ("----------------------")
    Debug.Print ("> " & xUmsatz)
    Debug.Print ("> " & xOLiP)
    Debug.Print ("> " & xSt1B)
    Debug.Print ("> " & xSt1P)
    Debug.Print ("> " & xSt2B)
    Debug.Print ("> " & xSt2P)
    Debug.Print ("> " & xSt3B)
    Debug.Print ("> " & xSt3P)
    Debug.Print ("> " & xSt4B)
    Debug.Print ("> " & xSt4P)
    Debug.Print ("----------------------")


    If xUmsatz > xSt4B And xSt4B <> "" Then
        Debug.Print ("Its tier 4!")
        PercentageInstallation = xSt4P

    ElseIf xUmsatz > xSt3B And xSt3B <> "" Then
        Debug.Print ("Its tier 3!")
        PercentageInstallation = xSt3P

    ElseIf xUmsatz > xSt2B And xSt2B <> "" Then
        Debug.Print ("Its tier 2!")
        PercentageInstallation = xSt2P

    ElseIf xUmsatz > xSt1B And xSt1B <> "" Then
        Debug.Print ("Its tier 1!")
        PercentageInstallation = xSt1P

    ElseIf xOLiP <> "" Then
        Debug.Print ("Its tier without Limit!")
        PercentageInstallation = xOLiP

    Else
        Call Error(4, xKdnr1)

    End If

End Function

我尝试将函数定义为Long,但这并没有解决问题。

尝试以下内容以了解不同值类型之间的差异:

Public Sub TestMe()

    Dim k As Long
    Dim p As Double

    k = 0.4
    Debug.Print k

    k = 0.7
    Debug.Print k

    p = 0.4
    Debug.Print p

End Sub

结果将出现在即时窗口中。

请阅读Visual Basic中的应用程序数据类型

基本数字类型的代码示例:

Sub Some_sub()

Dim a As Double
Dim b As Long
Dim c As Integer
Dim d As Single

a = 0.02 
b = 0.02 
c = 0.02 
d = 0.02 

Debug.Print a 'output: 0.02
Debug.Print b 'output: 0
Debug.Print c 'output: 0
Debug.Print d 'output: 0.02

End Sub

正如评论中所说:使用单倍或双倍。

使用
Single
Double
Long
只是另一种整数类型。所有这些都记录在文档中;这个问题毫无用处,也没有显示出太多的研究成果。我还应该提到,像第一行一样定义变量,只有最后一个变量实际上被定义为
长的
。其余部分定义为
Variant
。仅供参考,您的
Dim
语句仅将
xSt4P
声明为Long,而不是其余部分。您需要为每个变量指定类型,而不仅仅是末尾的变量。您会注意到调试打印是舍入此变量,而不是舍入其余变量。答案正确地指出了
Long
数据类型的问题,但我想我应该提到这一点。@Phylogenesis/A.S.H.谢谢。FWIW我只是为我的否决票辩护,没有投票结束(然而——肯定有人在某处被愚弄)。
Public Sub TestMe()

    Dim k As Long
    Dim p As Double

    k = 0.4
    Debug.Print k

    k = 0.7
    Debug.Print k

    p = 0.4
    Debug.Print p

End Sub
Sub Some_sub()

Dim a As Double
Dim b As Long
Dim c As Integer
Dim d As Single

a = 0.02 
b = 0.02 
c = 0.02 
d = 0.02 

Debug.Print a 'output: 0.02
Debug.Print b 'output: 0
Debug.Print c 'output: 0
Debug.Print d 'output: 0.02

End Sub