将整数和整数数组传递给 VBA 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16998043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:48:59  来源:igfitidea点击:

passing an integer and an array of integers to a VBA function

excel-vbavbaexcel

提问by musa yusuf

I'm trying to pass an integer and an array of integers to a function to check whether the integer is a member of the array.The arrowed line gives a compile error msg as: "expect:= ".

我试图将一个整数和一个整数数组传递给一个函数,以检查该整数是否是该数组的成员。箭头线给出了一个编译错误消息:“expect:=”。

Public Sub mainSub()
B = Array(4, 5, 6, 7, 8, 9)
ckArr(4,B) '<-------
End Sub

Public function ckArr(N As Integer, A() As Integer)
For i = 0 To UBound(A)
If N = A(i) And i <= UBound(A) Then
    Debug.Print N; " Is in the List"
    Exit For 'Do
ElseIf i < UBound(A) Then GoTo NXT
Else:
    Debug.Print N; " Is NOT in the List"
    Exit For 'Do
End If
NXT:
'i = i + 1
Next 'Loop
End function

回答by alk

To call a function either use:

要调用函数,请使用:

a = func(b,c)

or if you are not interessted in the result do:

或者如果您对结果不感兴趣,请执行以下操作:

call func(b, c)

or

或者

func b, c

Doing

正在做

func(b,c)

is not valid.

无效的



So in your particular case, it would be:

因此,在您的特定情况下,它将是:

Public Sub mainSub()  
  B = Array(4, 5, 6, 7, 8, 9)
  Call ckArr(4,B) '<-------
End Sub

or

或者

Public Sub mainSub()  
  B = Array(4, 5, 6, 7, 8, 9)
  ckArr 4, B '<-------
End Sub

回答by Santosh

Try below code:

试试下面的代码:

Public Sub mainSub()
    Dim IsMember As Boolean
    b = Array(4, 5, 6, 7, 8, 9)

    Dim checkVariable As Integer
    checkVariable = 4
    IsMember = ckArr(checkVariable, b)

    If IsMember Then
    MsgBox checkVariable & " is a member of member of array"
    Else
    MsgBox checkVariable & " is not a member of member of array"
    End If
End Sub

Public Function ckArr(N As Integer, A As Variant) As Boolean
    For i = LBound(A) To UBound(A)

        If N = A(i) Then
            ckArr = True
            Exit Function
        End If
    Next

End Function

enter image description here

在此处输入图片说明

B = Array(4, 5, 6, 7, 8, 9)so Bis variant here.

B = Array(4, 5, 6, 7, 8, 9)B这里的变体也是 如此。

So i have made parameter of below procedure as variant.

所以我将以下程序的参数作为变体。

From Public function ckArr(N As Integer, A() As Integer)

Public function ckArr(N As Integer, A() As Integer)

To Public Function ckArr(N As Integer, A As Variant) As Boolean

Public Function ckArr(N As Integer, A As Variant) As Boolean

回答by Santosh

Declaring B as integer Array

将 B 声明为整数数组

   Public Sub mainSub()

        Dim B(0 To 5) As Integer
        B(0) = 4
        B(1) = 5
        B(2) = 6
        B(3) = 7
        B(4) = 8
        B(5) = 9

        ckArr 4, B
    End Sub

    Public Function ckArr(N As Integer, A() As Integer)
        For i = 0 To UBound(A)
            If N = A(i) And i <= UBound(A) Then
                Debug.Print N; " Is in the List"
                Exit For    'Do
            ElseIf i < UBound(A) Then GoTo NXT
            Else:
                Debug.Print N; " Is NOT in the List"
                Exit For    'Do
            End If
    NXT:
            'i = i + 1
        Next    'Loop
    End Function