无法获取工作表函数类错误104的索引属性

时间:2018-04-20 16:20:27

标签: excel vba excel-vba

我正在尝试将随机选择返回到表单上的文本框中,并单击我在工作表中构建的范围内的按钮并继续收到此错误。这是我写的代码。

Private Sub CommandButton1_Click()

TextBox1.Value = Application.WorksheetFunction.Index("Codes!A3:K66", Application.WorksheetFunction.RandBetween(3, 64), Application.WorksheetFunction.RandBetween(1, 11))

End Sub

1 个答案:

答案 0 :(得分:0)

"Codes!A3:K66"必须为Worksheets("Codes").Range("A3:K66")

Private Sub CommandButton1_Click()

TextBox1.Value = Application.WorksheetFunction.Index("Codes!A3:K66", Application.WorksheetFunction.RandBetween(3, 64), Application.WorksheetFunction.RandBetween(1, 11))

End Sub

但你也可以这样做:

Private Sub CommandButton1_Click()
With Application.WorksheetFunction
    TextBox1.Value = Worksheets("Codes").Cells(.RandBetween(3, 66), .RandBetween(1, 11)).Value
End With
End Sub

跳过INDEX