在Excel VBA中为Simpson的1/3的子级,但出现错误(“需要对象”)

Excel_VAB

我正在为Simpson 1/3规则的子代码编写代码。我有该功能的代码,但是它与Sub中需要做的细微调整不起作用。Sub必须使用SimpsonResultCell.Value以下任一显示:

  • 一条消息(分数不是奇数时)或
  • 结果来自辛普森1/3方程。

要使用的消息是:“需要偶数个间隔(点数为奇数)”。

所以这是我的代码:

Sub Simpson13Sub()
    Dim xData As Range, yData As Range
    Dim x() As Double, y() As Double, h As Double, sum As Double
    Dim n As Integer, i As Integer, np As Integer, SimpsonResultCell As Integer

    Set xData = Application.InputBox("Select cell with x Data", Type:=8)
    Set yData = Application.InputBox("Select cell with y Data", Type:=8)
    Set SimpsonResultCell = Application.InputBox("Select cell to display result", Type:=8) ' object required appears here
    np = xData.Count 'number of points
    n = np - 1 'number of intervals

    If (n Mod 2 <> 0) Then
        SimpsonResultCell = "An even number of intervals is necessary (odd number of points)"
        Exit Sub
    Else
        Do
            ReDim x(0 To n)
            ReDim y(0 To n)
            For i = 0 To n
                x(i) = xData(i + 1)
                y(i) = yData(i + 1)
            Next i
                sum = y(0) + y(n)
            For i = 1 To n - 1 Step 2
            sum = sum + 4 * y(i)
            Next i
            For i = 2 To n - 2 Step 2
                sum = sum + 2 * y(i)
            Next i
                h = (x(n) - x(0)) / n
                SimpsonResultCell = h * sum / 3
        Exit Do
    End If
End Sub
共产国际

您的错误来自以下声明:

SimpsonResultCell As Integer

和这个任务:

Set SimpsonResultCell = Application.InputBox("Select cell to display result", Type:=8)

该语法Set Foo = Application.InputBox返回一个Range对象。您正在尝试将其分配给Integer。您需要做的就是使用适当的Dim语句:

Dim SimpsonResultCell As Range

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

尝试在 Excel VBA 中运行 MS Access 查询时出现“需要对象”错误

来自分类Dev

Excel VBA函数错误:“需要对象”

来自分类Dev

Excel VBA:运行时错误424,需要对象

来自分类Dev

VBA for excel 给出“未设置对象变量”或“需要对象”

来自分类Dev

Excel VBA宏:“对于每个...在...中”循环中的“需要对象”

来自分类Dev

VBA for Excel:需要错误424对象

来自分类Dev

运行时424,Excel宏VBA上需要对象

来自分类Dev

从Excel 2010升级到Excel 2013后,VBA中的大型阵列出现“内存不足”错误

来自分类Dev

Excel VBA中的对象必需错误

来自分类Dev

Excel VBA中的对象必需错误

来自分类Dev

Excel VBA脚本中的对象定义错误

来自分类Dev

VBA Excel-需要编译错误对象

来自分类Dev

VBA Excel-需要编译错误对象

来自分类Dev

需要运行时错误“424”对象:Excel VBA

来自分类Dev

Webscraping Excel初学者问题:需要对象

来自分类Dev

Excel VBA对象定义的错误

来自分类Dev

运行时错误13 VBA Excel

来自分类Dev

使用VBA(Excel)从XML DOM对象中删除(子)节点

来自分类Dev

Excel公式中的VBA错误

来自分类Dev

Excel VBA 代码中的错误

来自分类Dev

使用VBA时出现Excel Solver错误

来自分类Dev

Mac Excel 2011 VBA范围出现错误

来自分类Dev

Excel VBA函数出现#VALUE错误

来自分类Dev

Excel VBA QueryTable属性出现错误

来自分类Dev

VBA Excel出现错误时停止

来自分类Dev

循环遍历工作表中的Excel VBA范围对象错误

来自分类Dev

Excel VBA错误424调用子

来自分类Dev

Excel VBA:将命名范围存储在数组中时出现应用程序或对象定义错误

来自分类Dev

Excel VBA周期为