我有2个潜艇。第一个子读取工作表中的数据,并在计算后生成一个数组。在第二个,我想访问该数组(或数据)进一步修改,但是,到目前为止我还没有成功。一种解决方案是引入一个全局变量,但我会避免它。所以我的问题是如何从Sub Modify_MyCalculation运行Sub MyCalculation并将数据导入Get_DataArray数组。代码如下所示。
Option Explicit
Option Base 1
Sub MyCalculation()
' This code is giving output in MyCalcualtion
Dim CycleCounts As Variant
Dim StatusIndicator As Variant
Dim Calc() As Double
Dim NumberAtRisk() As Integer
Dim InverseRank() As Integer
Dim pj() As Double
Dim Rank() As Integer
Dim i As Integer
Dim DataArray As Variant
'Reads data from Table "TempData" in worksheet
CycleCounts = Range("TempData").Columns(1).Value
StatusIndicator = Range("TempData").Columns(2).Value
ReDim InverseRank(UBound(CycleCounts))
ReDim pj(UBound(CycleCounts))
ReDim Rank(UBound(CycleCounts))
ReDim Calc(UBound(CycleCounts))
For i = LBound(CycleCounts) To UBound(CycleCounts)
InverseRank(i) = (UBound(CycleCounts) + 1) - i
Rank(i) = i
Next i
For i = LBound(CycleCounts) To UBound(CycleCounts)
If StatusIndicator(i, 1) = 1 Then
pj(i) = Round(InverseRank(i + 1) / InverseRank(i), 3)
Else: pj(i) = 1
End If
Next i
Calc(1) = pj(1)
For i = 2 To UBound(CycleCounts)
Calc(i) = Round(pj(i) * Calc(i - 1), 3)
Next i
ReDim DataArray(LBound(CycleCounts) To UBound(CycleCounts), 1 To 3)
For i = 1 To 16
DataArray(i, 1) = CycleCounts(i, 1)
DataArray(i, 2) = StatusIndicator(i, 1)
DataArray(i, 3) = Calc(i)
Debug.Print DataArray(i, 1), DataArray(i, 2), DataArray(i, 3)
Next i
ReDim MyCalculation(LBound(CycleCounts) To UBound(CycleCounts), 1 To 3)
MyCalculation = DataArray
End Sub
Sub Modify_Mycalculation()
Dim Get_DataArray(1 To 16, 1 To 3) As Variant
Get_DataArray = MyCalculation
End Sub