2015-10-21 94 views
0

爲什麼這個函數在通過我的子程序後沒有返回任何數據?這個函數爲什麼不用數組返回一個數組?

需要的三個陣列是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 
+0

說實話有點混亂。如果你要從函數中返回多個變量,那麼你最好將它轉換爲Sub而不是Function。該函數的參數很奇怪。爲什麼第一個通過ByVal?你期望cA和cB是什麼變量數據類型(因爲它們是變體),而cC被定義爲Double。讓我們從這些問題開始...... –

+1

a_AR是函數的名稱,所以如果要分配一個值,則a_AR必須等於某個值。我的猜測是,不是'Returned_Array = a_AR',而是你可能會說'a_AR = aAR',也許完全擺脫了Returned_Array。 – Demetri

+0

您正在使用'aAr'和'a_AR' - 您需要後者根據函數返回數據。 – Rory

回答

2

也許您正在尋找類似的東西?

Public Function a_AR(ByVal cA, cB, ByVal cC As Double, cName As String, Optional cD As Double) As Variant() 

Lr = Range("A65000").End(xlUp).Row 
ReDim aAR(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 
a_AR = aAR 

End Function 
Sub Refactored_Macro() 
    'CNR array 
    Returned_Array_NR = ImportCNR_w_Paramaters("B2", "Entity ID", "Share Class", "Exchange Rate", "Net Assets") 
    'Nav rec array 
    Returned_Array_CNR = ImportCNR_w_Paramaters("B3", "ENTITY_ID", "LEDGER_ITEMS", "BALANCE_CHANGE") 
    'Relationship array 
    Returned_Array_Rel = ImportCNR_w_Paramaters("B4", "Hedge Entity Id", "Entity ID", "Share Class") 
End Sub 
+0

你是否改變了'a_AR = aAR'以外的任何東西? – phillipsK

+1

是的,我淘汰了「Returned_Array」,並且一次只將三個變量中的每一個設置爲等於一個函數。我不確定這是否是你想要做的,但我認爲你想讓數組1的值轉到Returned_Array_NR,數組2轉到Returned_Array_CNR,等等。所以對於每個變量,我只是將它設置爲等於該函數。如果這不太對,我希望它給你一些你可以嘗試的想法。 – Demetri