简体   繁体   English

Excel VBA-将公式结果分配给数组

[英]Excel VBA - Assign formula results to array

This works to put the values in the column: 这可以将值放在列中:

Sub JR_ArrayToDebugPint2()  
     ' written by Jack in the UK for [url]www.OzGrid.com[/url]  
     ' our web site [url]www.excel-it.com[/url]  
     ' Excel Xp+ 14th Aug 2004  
     ' [url]http://www.ozgrid.com/forum/showthread.php?t=38111[/url]  
    Dim JR_Values(500)  
    Dim JR_Count As Integer  
    Dim R As Long  
    R = 2  
    For JR_Count = 1 To 500 Step 1  
        JR_Values(JR_Count) = Evaluate("=INDEX('Client'!$O$2:$O$347473,MATCH(1,(('Client_Cost'!$D$2:$D$347473=BC" & CStr(R) & ")*('Client_Cost'!$E$2:$E$347473=BE" & CStr(R) & ")),0))")  
        Sheet1.Range("BG" & CStr(R) & "").Value = JR_Values(JR_Count)  
        R = R + 1  
        'Debug.Print JR_Values(JR_Count)  
        Next JR_Count  
End Sub

I've modified the original code I found on mrexcel.com 我已经修改了在mrexcel.com上找到的原始代码

I get the correct list of values whether I Debug.Print or print to the worksheet. 无论是Debug.Print还是打印到工作表,我都能获得正确的值列表。 So in my mind, I ought to be able to put the values in an array as they are calculated, then use Range("BG2:BG500").Value = Application.Transpose(myarray). 因此,在我看来,我应该能够将计算所得的值放入数组中,然后使用Range(“ BG2:BG500”)。Value = Application.Transpose(myarray)。

I am assuming if I do this the values will be placed in the cells in the column all at once, rather than one at a time, which is what this code, and all others I've tried, is doing. 我假设如果执行此操作,则这些值将一次全部放置在该列的单元格中,而不是一次放置一次,这就是该代码以及我尝试过的所有其他代码所执行的操作。 I am also assuming that, if the values are placed in the cells in the column all at once, it is MUCH faster than placing the values in the cells one at a time. 我还假设,如果将值一次全部放入列的单元格中,则比一次将值一次放入单元格中要快得多。

What I'm not able to do is get the code to put the value in an array once the formula is evaluated. 我无法执行的操作是获取公式计算后将值放入数组中的代码。 I've tried variations of the following with no success - statements to set the array and have the array take the value of the calculation are in caps and marked by ==>. 我尝试了以下各种变体,但均未成功-设置数组并让数组采用计算值的语句用大写表示,并用==>标记。 The most common error I get is type mismatch. 我最常见的错误是类型不匹配。

Sub JR_ArrayToDebugPint2()  
    Dim JR_Values(500)  
    Dim JR_Count As Integer  
    Dim R As Long  
 ==>   DIM arrPRICE(0 TO 500) AS VARIANT  
    R = 2  
    For JR_Count = 1 To 500 Step 1  
        JR_Values(JR_Count) = Evaluate("=INDEX('Client'!$O$2:$O$347473,MATCH(1,(('Client_Cost'!$D$2:$D$347473=BC" & CStr(R) & ")*('Client_Cost'!$E$2:$E$347473=BE" & CStr(R) & ")),0))")  
     ==>   arrPRICE(R) = JR_VALUES(JR_COUNT)  
        R = R + 1  
        'Debug.Print JR_Values(JR_Count)  
    Next JR_Count  
End Sub  

When you dimension the variant array like Dim JR_Values(500) you are creating a one-dimensioned array based upon a zero-based index. 当您为Dim JR_Values(500)类的变量数组Dim JR_Values(500)尺寸时,您将基于从零开始的索引创建一维数组。 This first element within the array is JR_Values(0) and the last is JR_Values(500) for a total of 501 array elements. 数组中的第一个元素是JR_Values(0) ,最后一个是JR_Values(500) ,总共501个数组元素。 While you could work from 0 to 499 with a little math, you can also force a one-based index on the variant array by declaring it that way. 虽然您可以在0到499的基础上进行一些数学运算,但是您也可以通过这样声明来在变体数组上强制基于1的索引。

The assumed worksheet parentage of the BC and BE columns where the individual row data criteria comes from is not definitive when using Application Evaluate like it is when the same formula is used on a worksheet. 当使用Application Evaluate时,假设假定工作表中的BC和BE列的工作表父母身份是不确定的,就像在工作表上使用相同公式时一样。 A worksheet knows who it is; 工作表知道是谁。 VBA may or may not know what worksheet you are implying. VBA可能会或可能不会知道您所暗示的工作表。

Sub JR_ArrayToDebugPint2()
    Dim olr As Long, rws As Long, JR_Count As Long, JR_Values As Variant

    'get some dimensions to the various data ranges
    With Worksheets("Client_Cost")
        'only use as many rows as absolutely necessary
        olr = Application.Min(.Cells(Rows.Count, "C").End(xlUp).Row, _
                              .Cells(Rows.Count, "E").End(xlUp).Row)
    End With
    With Worksheets("Client")
        rws = Application.Min(.Cells(Rows.Count, "BC").End(xlUp).Row, _
                              .Cells(Rows.Count, "BE").End(xlUp).Row)
        'override the above statement unless you want to run this overnight
        rws = 500
    End With

    ReDim JR_Values(1 To rws)   'force a one-based index on the array
    'Debug.Print LBound(JR_Values) & ":" & UBound(JR_Values)

    For JR_Count = LBound(JR_Values) To UBound(JR_Values) Step 1
        'Debug.Print Evaluate("INDEX('Client'!O2:O" & olr & _
                 ", MATCH(1, (('Client_Cost'!D2:D" & olr & "='Client'!BC" & JR_Count+1 & ")" & _
                            "*('Client_Cost'!E2:E" & olr & "='Client'!BE" & JR_Count+1 & ")), 0))")

        'R would be equal to JR_Count + 1 if R was still used (starts as R = 2)
        JR_Values(JR_Count) = _
          Evaluate("INDEX('Client'!O2:O" & olr & _
                 ", MATCH(1, (('Client_Cost'!D2:D" & olr & "='Client'!BC" & JR_Count + 1 & ")" & _
                            "*('Client_Cost'!E2:E" & olr & "='Client'!BE" & JR_Count + 1 & ")), 0))")
        'Debug.Print JR_Values(JR_Count)
    Next JR_Count

    With Worksheets("Client")
        .Range("BG2").Resize(UBound(JR_Values), 1) = Application.Transpose(JR_Values)
    End With

End Sub

I've left a lot of comments for you to review and subsequently clean up. 我留下了很多评论供您查看,然后进行清理。 I recently wrote a narrative of declaring one-dimension and two-dimension variant arrays in How to assign the variable length of an array to integer variable . 我最近在如何将数组的可变长度分配给整型变量中写了一个声明一维和二维变量数组的叙述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM