【问题标题】:Why isn't this function returning an array with data?为什么这个函数不返回一个包含数据的数组?
【发布时间】:2015-10-21 20:05:41
【问题描述】:

为什么这个函数在通过我的子过程后没有返回任何数据?

需要的三个数组是Returned_Array_CNRReturned_Array_NRReturned_Array_Rel As Variant

我是否使用Returned_Array = a_AR 正确执行了返回语句?

我的目标是用这个函数返回三个单独的数组,我希望通过函数头中定义的 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。
  • 您同时使用aAra_AR - 您需要后者将数据作为函数的结果返回。
  • 是的,是的。但是 OP 想要返回三个数组,而代码中只有两个是可能的(我猜)......
  • @Demetri 有效,但我需要在函数参数中传递 Returned Array,因为此参数允许我返回三个数组,如果没有此声明,我只能返回一个?

标签: arrays vba function excel


【解决方案1】:

也许您正在寻找类似的东西?

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 

【讨论】:

  • 除了a_AR = aAR 之外,您是否更改了其他内容?
  • 是的,我消除了“Returned_Array”,只是将三个变量中的每一个都设置为一次等于一个函数。我不确定这是否是您想要做的,但我假设您希望数组 1 的值转到 Returned_Array_NR,数组 2 的值转到 Returned_Array_CNR,依此类推。所以对于每个变量,我只是将它设置为等于函数。如果这不完全正确,我希望它能给你一些关于你可以尝试的想法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-21
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多