修剪Excel VBA的代码

时间:2016-03-11 20:47:54

标签: excel vba excel-vba macros

我正在尝试将其作为宏输入,但我遇到了麻烦,因为公式数组需要少于255个字符。我只想让我的宏用下面的数组填充table6中的所有单元格。有什么建议吗?

VBA:

Range("H31").Select
Selection.FormulaArray = _
    "=IFERROR(IF(IF([@[Generation Planted]]<>""F2"",INDEX(Table45,MATCH(1,([@[Trait(s)]]=Table45[TRAIT])*([@[Embryo/Seed]]=Table45[Input_type]),0),3),IF([@[Generation Planted]]=""F2"",INDEX(Table46,MATCH(1,([@[Trait(s)]]=Table46[TRAIT])*([@[Embryo/Seed]]=Table46[Input_type]),0),3),""""))=0,"""",IF([@[Generation Planted]]<>""F2"",INDEX(Table45,MATCH(1,([@[Trait(s)]]=Table" & _
    "])*([@[Embryo/Seed]]=Table45[Input_type]),0),3),IF([@[Generation Planted]]=""F2"",INDEX(Table46,MATCH(1,([@[Trait(s)]]=Table46[TRAIT])*([@[Embryo/Seed]]=Table46[Input_type]),0),3),""""))),"""")"
Selection.AutoFill Destination:=Range("Table6[Selection]"), Type:= _
    xlFillDefault
Range("Table6[Selection]").Select

我希望在H31:H2043(又名表6 [选择])中用作数组的公式:

=IFERROR(IF(IF([@[Generation Planted]]<>"F2",INDEX(Table45,MATCH(1,([@[Trait(s)]]=Table45[TRAIT])*([@[Embryo/Seed]]=Table45[Input_type]),0),3),IF([@[Generation Planted]]="F2",INDEX(Table46,MATCH(1,([@[Trait(s)]]=Table46[TRAIT])*([@[Embryo/Seed]]=Table46[Input_type]),0),3),""))=0,"",IF([@[Generation Planted]]<>"F2",INDEX(Table45,MATCH(1,([@[Trait(s)]]=Table45[TRAIT])*([@[Embryo/Seed]]=Table45[Input_type]),0),3),IF([@[Generation Planted]]="F2",INDEX(Table46,MATCH(1,([@[Trait(s)]]=Table46[TRAIT])*([@[Embryo/Seed]]=Table46[Input_type]),0),3),""))),"")

3 个答案:

答案 0 :(得分:0)

这是用您的公式填写“H31:H2043”范围:

Range("H3:H2043").Formula = "=IFERROR(IF(IF([@[Generation Planted]]<>"F2",INDEX(Table45,MATCH(1,([@[Trait(s)]]=Table45[TRAIT])*([@[Embryo/Seed]]=Table45[Input_type]),0),3),IF([@[Generation Planted]]="F2",INDEX(Table46,MATCH(1,([@[Trait(s)]]=Table46[TRAIT])*([@[Embryo/Seed]]=Table46[Input_type]),0),3),""))=0,"",IF([@[Generation Planted]]<>"F2",INDEX(Table45,MATCH(1,([@[Trait(s)]]=Table45[TRAIT])*([@[Embryo/Seed]]=Table45[Input_type]),0),3),IF([@[Generation Planted]]="F2",INDEX(Table46,MATCH(1,([@[Trait(s)]]=Table46[TRAIT])*([@[Embryo/Seed]]=Table46[Input_type]),0),3),""))),"")

答案 1 :(得分:0)

使用SendKeys (如果您的Windows / Excel版本支持SendKeys),您可以使用 VBA 将非常大的数组公式输入到单元格中。此处是一个非常简单的例子:

Sub dural()
    Dim BigString As String

    BigString = "=A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1 & A1"
    MsgBox Len(BigString)

    Range("B8").Formula = BigString

    Range("B8").Select
    Application.SendKeys "{F2}"
    Application.SendKeys "^+{ENTER}"
End Sub

enter image description here

诀窍是首先正常输入公式,然后应用 Ctrl + Shift + Enter

答案 2 :(得分:-1)

所以我只是在表格中创建了一个新列,列中的每一行都有数组,然后填充范围h等于r。 dee dee dee。