为什么这个函数在通过我的子程序后没有返回任何数据?这个函数为什么不用数组返回一个数组?
需要的三个阵列是Returned_Array_CNR
,Returned_Array_NR
,Returned_Array_Rel As Variant
。
我是否用Returned_Array = a_AR
正确执行return语句?
我打算用这个函数返回三个单独的数组,我希望通过函数头中定义的Returned_Array参数来标题。
我在函数的参数中传递了这个变量 - 我是否正确地做了这个?
Public Function a_AR(ByVal cA, cB, ByVal cC As Double, cName As String, Returned_Array As Variant, Optional cD As Double) As Variant()
Lr = Range("A65000").End(xlUp).row
ReDim aAR(1 To Lr, 1 To 4)
ReDim Returned_Array(1 To Lr, 1 To 4)
For r = 2 To Lr
cRow = cRow + 1
aAR(cRow, 1) = Sheets(1).Cells(r, cA) 'Fund Number
aAR(cRow, 2) = Sheets(1).Cells(r, cB) 'class
'Debug.Print aAR(cRow, 2) 'debugging
If cName = "Net Assets" Then
aAR(cRow, 3) = Sheets(1).Cells(r, cD)/Sheets(1).Cells(r, cC) 'TNA
Else
aAR(cRow, 3) = Sheets(1).Cells(r, cC)
End If
Next r
Returned_Array = a_AR
End Function
Sub Refactored_Macro()
'CNR array
ImportCNR_w_Paramaters "B2", "Entity ID", "Share Class", "Exchange Rate", Returned_Array_NR, "Net Assets"
'Nav rec array
ImportCNR_w_Paramaters "B3", "ENTITY_ID", "LEDGER_ITEMS", "BALANCE_CHANGE", Returned_Array_CNR
'Relationship array
ImportCNR_w_Paramaters "B4", "Hedge Entity Id", "Entity ID", "Share Class", Returned_Array_Rel
End Sub
Sub ImportCNR_w_Paramaters(cell As String, cName1, cName2, cName3 As String, Returned_Array2 As Variant, Optional cName4 As String)
MyPath = Range(cell) 'Defines cell that contains path to source that have been saved down
Workbooks.Open (MyPath) 'Opens workbook that have been saved down
Set tempbook = ActiveWorkbook 'Names workbook for future closing
'LR = Range("A65000").End(xlUp).row 'finds last row in edits
'ReDim aAR(1 To LR, 1 To 4)
cRow = 0
cName = cName1
cA = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
cName = cName2
cB = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
cName = cName3
cC = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
cName = cName4
cD = Cells.Find(What:=UCase(cName), After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Column
' For r = 2 To LR
' cRow = cRow + 1
' 'a_AR(cRow,r,cA,cB,cC,cD,cName)
a_Array = a_AR(cA, cB, cC, cName, Returned_Array2, cD)
'
' aAR(cRow, 1) = Sheets(1).Cells(r, cA) 'Fund Number
' aAR(cRow, 2) = Sheets(1).Cells(r, cB) 'class
' aAR(cRow, 3) = Sheets(1).Cells(r, cD)/Sheets(1).Cells(r, cC) 'TNA
'
' Next r
tempbook.Close
End Sub
说实话有点混乱。如果你要从函数中返回多个变量,那么你最好将它转换为Sub而不是Function。该函数的参数很奇怪。为什么第一个通过ByVal?你期望cA和cB是什么变量数据类型(因为它们是变体),而cC被定义为Double。让我们从这些问题开始...... –
a_AR是函数的名称,所以如果要分配一个值,则a_AR必须等于某个值。我的猜测是,不是'Returned_Array = a_AR',而是你可能会说'a_AR = aAR',也许完全摆脱了Returned_Array。 – Demetri
您正在使用'aAr'和'a_AR' - 您需要后者根据函数返回数据。 – Rory