ByRef参数types不匹配,Excel VBA
相当新的VBA,但在Java和C编程相当多。
我不幸的是似乎是一个经典的错误。 有这个问题的其他人似乎没有根本的问题。 原因是所有其他职位的错误,我已经find了函数调用标志。 我的错误标志在函数定义!
声明:
Dim aktier() As String ReDim aktier(antalTr) As String Dim unikaAktier() As String ReDim unikaAktier(antalTr) As String Dim antalTr As Integer Dim Kop As Integer Kop = 1 Dim Salj As Integer Salj = 2 Dim antalUnika As Integer antalUnika = 0
函数调用for循环。 这里没有错误标志。
For o = 1 To antalUnika Cells(o + 1, 3).Value = snittInkopEllerSalj(unikaAktier(o), antalTr, Kop) Cells(o + 1, 4).Value = snittInkopEllerSalj(unikaAktier(o), antalTr, Salj) Next o
function。 在第一行标记。
Function snittInkopEllerSalj(bolag As String, antalTr As Integer, InUt As Integer) As Integer Dim totKopSalj As Integer Dim totAktier As Integer totKopSalj = 0 totAktier = 0 For i = 1 To antalTr If Sheet1.Cells(i, 4).Value = bolag Then If InUt = 1 Then If isCorrectTrans(i, InUt) = True Then totKopSalj = totKop + Sheet1.Cells(i, 7) totAktier = totAktier + Sheet1.Cells(i, 5) End If Else If isCorrectTrans(i, InUt) = True Then totKopSalj = totKop + Sheet1.Cells(i, 7) totAktier = totAktier + Sheet1.Cells(i, 5) End If End If End If Next i snittInkopEllerSalj = Round(totKopSalj / totAktier) End Function
我真的不明白为什么我得到这个错误。 我觉得所有的东西都是明确的?
您应该始终将Option Explicit
放在所有模块的第一行。 这强制所有的variables必须声明。
如果你添加所有缺less的variables声明你的代码应该工作。 而且,在使用ReDim aktier(antalTr) As String
之前,必须先声明ReDim aktier(antalTr) As String
与您的问题无关,但是:
- 你不应该使用
Integer
types。 使用Long
而不是。 由于VBA中的整数只有16位,但是现在计算机没有16位寄存器,它将以32位长的长度存储。 另外你不必担心打破32767的边界。 - 明确的范围你的function。 如果你不预先
Private
或Public
,后者将是默认的。 - 所有参数默认情况下都会通过引用(
ByRef
)传递。 如果您不希望在Sub或Function声明中的参数名称之前放置一个ByVal
。