从不同的子元素返回变量不会保留值

时间:2018-03-02 21:52:00

标签: vba excel-vba powerpoint-vba excel

在这里,我调用一个子例程,它将为我做excel中的一些事情,我希望填充IQRef()IQRngRef()

    Dim IQRef() As Variant
    Dim IQRngRef() As Variant
    CaptureExcelReferences xlWB, pivotSheetName, IQRef, IQRngRef

以下是Sub例程,它要求CaptureIQRefsLocally填写IQRef()IQRngRef()

Private Sub CaptureExcelReferences(ByVal xlWB As Workbook, ByVal pivotSheetName As String, ByRef IQRef() As Variant, ByRef IQRngRef() As Variant)
    Dim ShRef As Excel.Worksheet
    Set ShRef = xlWB.Worksheets(pivotSheetName)

    Dim colNumb As Long
    colNumb = ShRef.Cells(1, ShRef.Columns.Count).End(xlToLeft).Column

    ReDim IQRef(colNumb)
    ReDim IQRngRef(colNumb)

    Dim rowNumb As Long
    rowNumb = ShRef.Cells(ShRef.Rows.Count, 1).End(xlUp).Row

    CaptureIQRefsLocally ShRef, rowNumb, colNumb, IQRef, IQRngRef
    IdentifyRolesAndScoresRows IQRngRef, rowNumb
End Sub

以下是实际填写IQRef()IQRngRef()

的Sub
Private Sub CaptureIQRefsLocally(ByVal ShRef As Worksheet, ByVal rowNumb As Long, ByVal colNumb As Long, ByRef IQRef As Variant, ByRef IQRngRef As Variant)
    'capture IQ references in arrays. Values for column titles in IQRef and full column Ranges in IQRngRef.
    Dim iCol As Long
    Dim alignIQNumbToArrayNumb
        For iCol = 1 To colNumb
            alignIQNumbToArrayNumb = iCol - 1
            Set IQRngRef(alignIQNumbToArrayNumb) = ShRef.Range(ShRef.Cells(1, iCol), ShRef.Cells(rowNumb, iCol))
            IQRef(alignIQNumbToArrayNumb) = ShRef.Cells(1, iCol).Value
            'IsThisaKeyIQ IQRngref, IQRef
        Next iCol
End Sub

IQRef的一切正常,它被正确填充并完全和完全传回主Sub(每个数组槽都填充一个单元格的String,因此它为每个槽返回一个字符串。 )

对于IQRngRef,每个插槽都填充了一个范围,结果不太好。在CaptureIQRefsLocally中,它会被正确填充并正确地将值传递回CaptureExcelReferences,它甚至可以正确地用于以下子IdentifyRolesAndScoresRows(它不会在此子句中被修改完全没有,并且返回值时值仍然正确)...但是当它被传递回主子IQRngRef时它是空的......是的。它看起来像这样:enter image description here

发生这种情况的原因(正如我在输入时发现的那样)是因为我在这个sub之后关闭了excel工作簿和应用程序,所以我可以回到powerpoint工作(我只想进入excel,获取我需要的信息,将其存储在数组中并关闭它。)有没有办法在数组中保存所有相同的值并关闭excel应用程序?

1 个答案:

答案 0 :(得分:0)

使用

IQRngRef(alignIQNumbToArrayNumb) = Application.Transpose(ShRef.Range(ShRef.Cells(1, iCol), ShRef.Cells(rowNumb, iCol)).Value)

这样每个IQRngRef插槽都会存储一维数组