gpt4 book ai didi

excel - if block 的应用程序定义或对象定义错误

转载 作者:行者123 更新时间:2023-12-04 21:18:40 25 4
gpt4 key购买 nike

我在 VBA 中编写了下面的代码,它基本上检查单元格 B24 中的值是否为正,如果为正,它将通过 for 循环迭代 50 次,其中 X0 和 sqrt 被重复更新。
但是,当我调试代码时,它会引发错误

'Run-time error '1004'
Application-defined or object-defined error.
我检查了语法,它看起来不错,但错误不断弹出。如何解决这个问题?请帮助
Sub forloopCalc()

Dim X0, sqrt As Integer
Dim sheetName As String
sheetName = "Sheet1"
Dim WSD As Worksheet
Set WSD = Worksheets(sheetName)


If (Cells(B, 24).Value > 0) Then
X0 = 5.96046447753906E-08
sqrt = 0.000244140625
For i = 0 To 50
If ((X0 - Cells(B, 24).Value) < 0) Then
X0 = X0 * 4
sqrt = sqrt * 2
Exit For
End If
Next i
End If

WSD.Cells(P, 24).Value = X0
WSD.Cells(R, 24).Value = sqrt


End Sub

最佳答案

在这一行(和其他行):

If (Cells(B, 24).Value > 0) Then
B被视为具有初始值 0 的变量,而不是作为文字行引用。还有 "B"只能用于列,不能用于行。还有单元格 B24 B 列第 24 行 , 以及 Cells 中的参数是 rowIndex, colIndex .
写:
If (Cells(24, "B").Value > 0) Then
反而。

关于excel - if block 的应用程序定义或对象定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64059174/

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