a(3) 为已知数组,c()为一动态数组,保存值相同的所有元素的下标
Private Sub Command1_Click()
Dim c() As Integer
Dim a(3) As Integer
Dim i As Integer
Dim k As Boolean
a(0) = 1
a(1) = 2
a(2) = 1
a(3) = 1
k = MyFun(a, 1, c)
If k Then
For i = 0 To UBound(c)
Debug.Print c(i)
Next
Else
MsgBox "没有相等的数据"
End If
End SubFunction MyFun(a() As Integer, b As Integer, ByRef c() As Integer) As Boolean Dim i As Integer
Dim k As Integer
k=0
For i = 0 To UBound(a)
If b = a(i) Then
ReDim Preserve c(k)
c(k) = i
k = k + 1
End If
Next
If k > 0 Then
MyFun = True
Else
MyFun = False
End If
End Function
Private Sub Command1_Click()
Dim c() As Integer
Dim a(3) As Integer
Dim i As Integer
Dim k As Boolean
a(0) = 1
a(1) = 2
a(2) = 1
a(3) = 1
k = MyFun(a, 1, c)
If k Then
For i = 0 To UBound(c)
Debug.Print c(i)
Next
Else
MsgBox "没有相等的数据"
End If
End SubFunction MyFun(a() As Integer, b As Integer, ByRef c() As Integer) As Boolean Dim i As Integer
Dim k As Integer
k=0
For i = 0 To UBound(a)
If b = a(i) Then
ReDim Preserve c(k)
c(k) = i
k = k + 1
End If
Next
If k > 0 Then
MyFun = True
Else
MyFun = False
End If
End Function
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货