从VBA中的复选框应用公式

时间:2015-11-18 21:34:34

标签: vba checkbox excel-formula

我现在已经挣扎了一段时间了。我有这段代码:

 Private Sub CheckBox1_Click()
 If OptionButton1.Value = True Then
 Sheets("Sheet1").Select
 Range("J4").Select
 Selection.FormulaArray = "=IFERROR(INDEX(C$2:C$14, SMALL(IF($B$2:$B$14=1, ROW($B$2:$B$14)-1),ROWS(G$2:G2))),"")"
 Selection.Columns.AutoFit

 ElseIf OptionButton1.Value = False Then
 Range("J4").Value = " "
 End If

 End Sub

我试图在单元格J4中执行公式。它只是赢了!而且我真的不明白为什么!

2 个答案:

答案 0 :(得分:0)

数组公式must be R1C1 format not A1

所以:

Private Sub CheckBox1_Click()
If OptionButton1.Value = True Then
    Sheets("Sheet1").Range("J4").FormulaArray = "=IFERROR(INDEX(R2C[-9]:R14C[-9], SMALL(IF(R2C2:R14C2=1, ROW(R2C2:R14C2)-1),ROWS(R2C[-5]:R[-2]C[-5]))),"""")"
    Sheets("Sheet1").Range("J4").Columns().AutoFit
    Sheets("Sheet1").Range("J4:J40").AutoFill
ElseIf OptionButton1.Value = False Then
    Range("J4").Value = " "
End If

End Sub

答案 1 :(得分:0)

不确定这是否是唯一的问题,但请考虑更改此行:

Selection.FormulaArray = "=IFERROR(INDEX(C$2:C$14, SMALL(IF($B$2:$B$14=1, ROW($B$2:$B$14)-1),ROWS(G$2:G2))),"")"

到此:

Selection.FormulaArray = "=IFERROR(INDEX(C$2:C$14, SMALL(IF($B$2:$B$14=1, ROW($B$2:$B$14)-1),ROWS(G$2:G2))),"""")"