4

我正在尝试将一个整数和一个整数数组传递给一个函数,以检查该整数是否是数组的成员。箭头线给出了一个编译错误消息:“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
4

3 回答 3

5

要调用函数,请使用:

a = func(b,c)

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

call func(b, c)

或者

func b, c

正在做

func(b,c)

无效。_


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

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

或者

Public Sub mainSub()  
  B = Array(4, 5, 6, 7, 8, 9)
  ckArr 4, B '<-------
End Sub
于 2013-06-08T10:07:04.060 回答
3

试试下面的代码:

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

在此处输入图像描述

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

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

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

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

于 2013-06-08T09:39:41.090 回答
2

将 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
于 2013-06-08T09:49:38.653 回答