调用公共数组

问题描述 投票:0回答:1

我在 vba 代码中使用公共数组,我在 Test_String_Signaling 宏中填充了值,然后我想在另一个宏中调用它的值,但错误下标超出范围 这是我填写公共数组值的地方:

Public Sum_N_User_Inactivity_Per_CallID() As Variant
Public Sum_N_Handover_Per_CallID() As Variant
Public Sum_N_Eutran_Generated_Reason_Per_CallID() As Variant
Public Sum_N_Normal_Release_Per_CallID() As Variant
Public Sum_N_Authentication_Failure_Per_CallID() As Variant
Public Sum_N_Detach_Per_CallID() As Variant
Public Sum_N_Unspecified_Per_CallID() As Variant
Public Sum_N_Radio_Cx_With_UE_Lost_Per_CallID() As Variant
Public Sum_N_CS_FallBack_Triggered_Per_CallID() As Variant
Public Sum_N_Not_Enlisted_Reason_Per_CallID() As Variant

Sub Test_String_Signaling()
Dim N_Calls As Long
  N_Calls = Temporary.Range("A2").End(xlDown).Row
ReDim Sum_N_User_Inactivity_Per_CallID(0 To N_Calls - 1)
' here there is a code that analyzes a string and then for each value
'    i increment a specific counter and this is an example of one counter:
 If Drop_Cause(Sigma_Lines(l - 1) + k + 2) = "02 02 80" Then
    N_User_Inactivity_Per_CallID = N_User_Inactivity_Per_CallID + 1
    'k is going from 0 to a maximum value previously calculated
     ' then after breaking from the loop k i put the result of the counter in the array like that:

Sum_N_User_Inactivity_Per_CallID(l) = N_User_Inactivity_Per_CallID
Debug.Print ("Sum_N_User_Inactivity_Per_CallID = " & Sum_N_User_Inactivity_Per_CallID(l))
Sum_N_Handover_Per_CallID(l) = N_Handover_Per_CallID
Sum_N_Eutran_Generated_Reason_Per_CallID(l) = N_Eutran_Generated_Reason_Per_CallID
Sum_N_Normal_Release_Per_CallID(l) = N_Normal_Release_Per_CallID
Sum_N_Authentication_Failure_Per_CallID(l) = N_Authentication_Failure_Per_CallID
Sum_N_Detach_Per_CallID(l) = N_Detach_Per_CallID
Sum_N_Unspecified_Per_CallID(l) = N_Unspecified_Per_CallID
Sum_N_Radio_Cx_With_UE_Lost_Per_CallID(l) = N_Radio_Cx_With_UE_Lost_Per_CallID
Sum_N_CS_FallBack_Triggered_Per_CallID(l) = N_CS_FallBack_Triggered_Per_CallID
Sum_N_Not_Enlisted_Reason_Per_CallID(l) = N_Not_Enlisted_Reason_Per_CallID

然后我在查找我测试并正确返回的索引“l”后调用这些值,

dim N_User_Inactivity_Per_CellID as integer
N_User_Inactivity_Per_CellID=0
With Temporary
    Set FindRow = Temporary.Range("A:A").Find( _
         What:=Synthese_Global.Cells(i, 13).Value, LookIn:=xlValues)
End With
FindRowNumber = FindRow.Row

N_User_Inactivity_Per_CellID = N_User_Inactivity_Per_CellID + _
             Sum_N_User_Inactivity_Per_CallID(FindRowNumber - 2)

我认为它在考虑其他宏 Test_String_Signaling 的值时存在问题 非常感谢任何帮助!

excel vba
1个回答
0
投票

“你能给我看一个例子,你声明一个公共数组并将其填充到macro1中,然后在macro2中调用它的值吗?”

例如:

Option Explicit

Public Sum_N_User_Inactivity_Per_CallID() As Variant

Sub Tester()
    
    ReDim Sum_N_User_Inactivity_Per_CallID(1 To 3)
    
    Sum_N_User_Inactivity_Per_CallID(1) = "one"
    Sum_N_User_Inactivity_Per_CallID(2) = "two"
    Sum_N_User_Inactivity_Per_CallID(3) = "three"
    
    Tester2
End Sub

Sub Tester2()
    Dim i As Long
    For i = LBound(Sum_N_User_Inactivity_Per_CallID) To UBound(Sum_N_User_Inactivity_Per_CallID)
        Debug.Print i, Sum_N_User_Inactivity_Per_CallID(i)
    Next i
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.