简体   繁体   English

将VBA数组分配给excel范围的公式时的奇怪行为

[英]Strange behavior when assigning a VBA array to formulas of an excel range

This is strange... Someone has some knowledge or can figure out why is this happening? 这很奇怪......有人知道或者可以弄清楚为什么会这样?

In the past, I was able to assign an array of formulas to an Excel range ( Example of fast assignment: Range("A1:A1000") = ArrayOfFormulas ). 在过去,我能够将一组公式分配给Excel范围( 快速分配的示例: Range("A1:A1000") = ArrayOfFormulas )。 It was working fine and FAST ( very FAST ) compared to assign cell by cell ( Example cell by cell: Range("A" & i).Formula=ArrayOfFormula(i) inside a loop. Is slow! ). 与逐个细胞分配相比,它运行良好且快速非常 )(逐个细胞的示例: Range("A" & i).Formula=ArrayOfFormula(i)在循环内。很慢! )。

I'm working with Excel 2013 now, and when trying to assign the formulas stored in an array of strings to an excel range it doesn't work (The excel shows the formulation but not the calculation as per left part of the picture below) an unfortunately I cannot retrieve old code to compare. 我现在正在使用Excel 2013,并且当尝试将存储在字符串数组中的公式分配给excel范围时,它不起作用(excel显示公式,但不是根据下图左侧部分的计算)遗憾的是,我无法检索旧代码进行比较。 For example the following code shows "=1+2" instead of "3" in the range A1 to A1000. 例如,以下代码在A1到A1000的范围内显示“= 1 + 2”而不是“3”。

Sub AssignFormulas_1()
    Dim i as Long
    Dim FORML_ARRAY(1 To 1000, 1 To 1) As String
    For i = 1 To 1000
         FORML_ARRAY(i, 1) = "=1+2"
    Next i
    Range("A1:A1000").Formula = FORML_ARRAY  '<- Don't work as formula
                                             '   It put the value!
End Sub

However, when I don't use the array of formulas it work fine (all cells shows "3" instead of "=1+2" as per the right part of the picture below). 但是,当我不使用公式数组时,它工作正常(所有单元格显示“3”而不是“= 1 + 2”,如下图右侧所示)。 Here the code: 这里的代码:

Sub AssignFormulas_2()
    Dim i as Long
    Dim FORML_SINGLE As String
    FORML_SINGLE = "=1+2"
    Range("A1:A1000").Formula = FORML_SINGLE '<- works ok, not practical for my
                                             '   real life case as I need 
                                             '   different formula for each cell.
End Sub

The following code work also well (In this case I assign formulas cell by cell, so is more flexible but sloooowly for large formulas). 下面的代码也可以正常工作(在这种情况下,我逐个单元地分配公式,因此对于大型公式来说更灵活但是很有用)。

Sub AssignFormulas_3()
    Dim i as Long
    Dim FORML_ARRAY(1 To 1000, 1 To 1) As String
    For i = 1 To 1000
         FORML_ARRAY(i, 1) = "=1+2"
         Range("A1:A" & i).Formula = FORML_ARRAY(i, 1) '<- works ok, but slowly
    Next i
End Sub

The pictures with the output of all above code: 上面所有代码输出的图片:

结果示例错误(左)和正确(右)


Note: The cells were properly formatted as number, not text! 注意:单元格格式正确,不是文本格式!

your first code should work. 你的第一个代码应该工作。
Instead of declaring it as string, declare it as variant. 而不是将其声明为字符串,将其声明为变体。

So change your declaration from this: 所以改变你的声明:

Dim FORML_ARRAY(1 To 1000, 1 To 1) As String

to this: 对此:

Dim FORML_ARRAY(1 To 1000, 1 To 1) As Variant

I have no way of trying it on Excel 2013, but it should work. 我无法在Excel 2013上尝试它,但它应该工作。
Hope it does. 希望它能做到。

Interesting question. 有趣的问题。 I've never used this functionality before. 我以前从未使用过这个功能。

Your first one has the same result for me in both 2010 and 2013 - a column containing the string "=1+2" . 在2010年和2013年,您的第一个结果与我相同 - 包含字符串"=1+2" I changed FORML_ARRAY from a string to a Variant and it works as expected. 我将FORML_ARRAY从字符串更改为Variant ,它按预期工作。 For good measure I changed the formula to include i and it still fills the range with formulas: 为了更好的衡量,我将公式更改为包含i ,它仍然使用公式填充范围:

Sub AssignFormulas_1()
    Dim i As Long
    Dim FORML_ARRAY(1 To 1000, 1 To 1) As Variant 'changed to Variant
    For i = 1 To 1000
         FORML_ARRAY(i, 1) = "=" & i & "+2" 'changed 1 to i - still works
    Next i
    Range("A1:A1000").Formula = FORML_ARRAY
End Sub

Just to add another option, cause as you said you would rather dim your array correctly, With this you can, it only adds 1/250th of a second to the OVERALL Sub ( not per loop), but will allow you to dim your array as a string. 只是为了添加另一个选项,因为你说你宁愿正确地调暗你的数组,你可以这样做,它只会给OVERALL Sub( 不是每个循环)增加1/250秒,但是会让你调暗你的数组作为一个字符串。

Sub AssignFormulas_2()
    Dim i As Long

    Dim FORML_ARRAY(1 To 1000, 1 To 1) As String

    For i = 1 To 1000
         FORML_ARRAY(i, 1) = "= " & i & " + 2"
    Next i

    Range("A1:A1000").Formula = FORML_ARRAY  '<- Don't work as formula
                                             '   It put the value!

    Range("A1:A1000").Value = Range("A1:A1000").Value
End Sub

Now, I am not sure why excel reads the strings as constants, But if you run your original formula and go to Evaluate Formula, it gives the error that the cell contains a constant, This really just seems to be a bug, but If i figure something else out I will edit my answer also. 现在,我不确定为什么excel将字符串作为常量读取,但是如果你运行原始公式并转到Evaluate Formula,它会给出单元格包含常量的错误,这真的只是一个bug,但是如果我想出别的东西我也会编辑我的答案。

NOTE: This answer is not intended to be a BETTER answer as the accepted answer, it is a slower answer and is more of an alternative; 注意:这个答案不是一个更好的答案,因为它是一个接受的答案,它是一个较慢的答案,更多的是另一种选择; to answer the request that you wanted to dim the array a string. 回答你想要将数组调暗一个字符串的请求。

UPDATE: This method will split the difference between the accepted answer and the above method. 更新:此方法将分割接受的答案和上述方法之间的差异。 This will only add 1/500th of a second to the sub's run time. 这只会增加子运行时间的1/500秒。

Sub AssignFormulas_Transpose()
    Dim i As Long

    Dim FORML_ARRAY(1 To 1000) As String
    For i = 1 To 1000
         FORML_ARRAY(i) = "= " & i & " + 2"
    Next i

    Range("A1:A1000").Formula = Application.Transpose(FORML_ARRAY)  '<- Don't work as formula
                                                                    '   It put the value!
End Sub

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

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