gpt4 book ai didi

vba - Excel VBA 错误 438 : object doesn't support this property or method

转载 作者:行者123 更新时间:2023-12-03 02:47:21 25 4
gpt4 key购买 nike

请帮忙,这是我第一次尝试用 VBA 编写一些有用的东西,我现在正在自学。我得到了上面的错误。请帮忙

Sub Bezier()
Dim C As Double, k As Integer, ansx As Double, ansy As Double, t As Double, n As Integer
n = 3
For i = 0 To 100
t = i * 0.01
ansx = 0
ansy = 0
For k = 0 To n
C = (WorksheetFunction.Fact(n) / WorksheetFunction.Fact(k)) / WorksheetFunction.Fact(n - k)
ansx = ansx + Cells(k + 2, 1).Value * C * WorksheetFunction.Power(t, k) * WorksheetFunction.Power(1 - t, n - k)
ansy = ansy + Cells(k + 2, 2).Value * C * WorksheetFunction.Power(t, k) * WorksheetFunction.Power(1 - t, n - k)
Next
Cells(i + 2, 6).Value = ansx
Cells(i + 2, 7).Value = ansy
Next

End Sub

最佳答案

首先,您应该知道,工作表上使用的某些函数是有限制的。所以我的观点是,如果没有必要,请避免在 VBA 中使用它们。
例如,函数POWER()在尝试将零提高到零时返回错误。另一种方法是使用 0 ^ 0 组合,其作用完全相同,但看起来更简单并且操作不会出现此类错误。
但 VBA 中也没有嵌入 FACT() 函数的替代方案,因此您可以使用它,或者只需添加您自己的 function Factor() -这取决于您的选择。

如果您刚刚开始学习 VBA,我建议您使用Option Explicit。它将帮助您找出哪些变量未定义,有时还可以避免与变量名打印错误相关的错误。

这是您的代码,已修复并进行了一些优化:

Option Explicit' It is an option that turns on check for every used variable to be defined before execution. If this option is not defined, your code below will find undefined variables and define them when they are used. Good practice is to use this option, because it helps you, for example to prevent missprinting errors in variable names.

Sub Bezier()
Dim C as Double , t As Double
Dim k As Long, n As Long, i As Long
n = 3
For i = 0 To 100
t = i * 0.01
Cells(i + 2, 6) = 0
Cells(i + 2, 7) = 0
For k = 0 To n
C = (WorksheetFunction.Fact(n) / WorksheetFunction.Fact(k)) / WorksheetFunction.Fact(n - k)
Cells(i + 2, 6) = Cells(i + 2, 6).Value + Cells(k + 2, 1).Value * C * (t ^ k) * ((1 - t) ^ (n - k))
Cells(i + 2, 7) = Cells(i + 2, 7).Value + Cells(k + 2, 2).Value * C * (t ^ k) * ((1 - t) ^ (n - k))
Next
Next
End Sub

更新
以下是阶乘计算的一些示例。

    Public Function fnFact(number) ' a simple cycle example of Factorial function
Dim tmp As Long ' new temporary variable to keep the "number" variable unchanged
tmp = number
fnFact = number
While tmp > 1
tmp = tmp - 1
fnFact = fnFact * tmp
Wend
End Function

Public Function fnFactR(number) ' a simple example of recursive function for Factorial calculation
If number > 0 Then
fnFactR = fnFactR(number - 1) * number ' function calls itself to continue calculations
Else
fnFactR = 1 ' function returns {1} when calculations are over
End If
End Function

Sub Factor_test() 'RUN ME TO TEST ALL THE FACTORIAL FUNCTIONS
Dim number As Long
number = 170 ' change me to find Factorial for a different value
MsgBox "Cycle Factorial:" & vbNewLine & number & "!= " & fnFact(number)
MsgBox "WorksheetFunction Factorial:" & vbNewLine & number & "!= " & WorksheetFunction.Fact(number)
MsgBox "Recursive Factorial:" & vbNewLine & number & "!= " & fnFactR(number)
End Sub

由于结果值较大,所有这些函数仅可用于计算 170 之前的数字的阶乘。
因此,对于我的 PC,WorksheetFunction.Fact() 函数的限制也是 170。
让我知道,如果您的电脑对此功能有不同的限制,-这是非常有趣的事情。 :)

更新2
当需要整数(或整数)变量时,建议使用Long数据类型而不是Integer每种类型。 Long 稍微快一点,它有更广泛的限制,并且不需要额外的内存。以下是证明链接:
1.MSDN:The Integer, Long, and Byte Data Types
2.ozgrid.com:Long Vs Integer
3.pcreview.co.uk:VBA code optimization - why using long instead of integer?
感谢 @Ioannis@chris neilsen 了解有关 Long 数据类型和证明链接的信息!

祝您进一步的 VBA 操作顺利!

关于vba - Excel VBA 错误 438 : object doesn't support this property or method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25329757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com