VBA FormulaArray - 无法设置Range类的FormulaArray属性

时间:2017-02-08 07:58:30

标签: excel vba excel-vba excel-formula

首先,我正在尝试编写一个宏程序,让用户可以使用它来查找最接近用户输入的总目标的数字。

  • 在第一张图中,数量列分别显示其各自标题的金额。目标是来自用户的输入。水平A,B,C,D用于向用户显示使用的标题。

enter image description here

文件执行后我收到此错误消息。

enter image description here

有什么建议吗?感谢...

Sub Option1()
    Dim c As Long
    For c = 5 To 8
        Sheet1.Cells(5, c).FormulaArray = _
          "=INDEX(MOD(INT((ROW(R2C2:INDEX(C2,2^ROWS(R2C2:R9C2)))-1)/2^(TRANSPOSE(MATCH(ROW(R2C2:R9C2),ROW(R2C2:R9C2)))-1)),2)*TRANSPOSE(R2C2:R9C2),MATCH(MIN(ABS(MMULT(MOD(INT((ROW(R2C2:INDEX(C2,2^ROWS(R2C2:R9C2)))-1)/2^(TRANSPOSE(MATCH(ROW(R2C2:R9C2),ROW(R2C2:R9C2)))-1)),2),R2C2:R9C2)-R1C5)),ABS(MMULT(MOD(INT((ROW(R2C2:INDEX(C2,2^ROWS(R2C2:R9C2)))-1)/2^(TRANSPOSE(MATCH(ROW(R2C2:R9C2),ROW(R2C2:R9C2)))-1)),2),R2C2:R9C2)-R1C5),0),0)"
    Next c
End Sub

1 个答案:

答案 0 :(得分:0)

您应该直接申请整个范围,而不是每个单元格:

Sub Option1()
    Sheet1.Range(Sheet1.Cells(5,5),Sheet1.Cells(5, 8)).Clear
    Sheet1.Range(Sheet1.Cells(5,5),Sheet1.Cells(5, 8)).FormulaArray = _
       "=INDEX(MOD(INT((ROW(R2C2:INDEX(C2,2^ROWS(R2C2:R9C2)))-1)/2^(TRANSPOSE(MATCH(ROW(R2C2:R9C2),ROW(R2C2:R9C2)))-1)),2)*TRANSPOSE(R2C2:R9C2),MATCH(MIN(ABS(MMULT(MOD(INT((ROW(R2C2:INDEX(C2,2^ROWS(R2C2:R9C2)))-1)/2^(TRANSPOSE(MATCH(ROW(R2C2:R9C2),ROW(R2C2:R9C2)))-1)),2),R2C2:R9C2)-R1C5)),ABS(MMULT(MOD(INT((ROW(R2C2:INDEX(C2,2^ROWS(R2C2:R9C2)))-1)/2^(TRANSPOSE(MATCH(ROW(R2C2:R9C2),ROW(R2C2:R9C2)))-1)),2),R2C2:R9C2)-R1C5),0),0)"

End Sub

Sub Option2()
    Sheet1.Range("E5:I5").Clear
    Sheet1.Range("E5:I5").FormulaArray = _
       "=INDEX(MOD(INT((ROW(R2C2:INDEX(R2C3,2^ROWS(R2C2:R9C2)))-1)/2^(TRANSPOSE(MATCH(ROW(R2C2:R9C2),ROW(R2C2:R9C2)))-1)),2)*TRANSPOSE(R2C2:R9C2),MATCH(MIN(ABS(MMULT(MOD(INT((ROW(R2C2:INDEX(R2C3,2^ROWS(R2C2:R9C2)))-1)/2^(TRANSPOSE(MATCH(ROW(R2C2:R9C2),ROW(R2C2:R9C2)))-1)),2),R2C2:R9C2)-R1C5)),ABS(MMULT(MOD(INT((ROW(R2C2:INDEX(R2C3,2^ROWS(R2C2:R9C2)))-1)/2^(TRANSPOSE(MATCH(ROW(R2C2:R9C2),ROW(R2C2:R9C2)))-1)),2),R2C2:R9C2)-R1C5),0),0)"

End Sub