I am currently writing a program that contains a two dimensional newton raphson sub. it starts like this:
我目前正在编写一个包含二维牛顿raphson子程序的程序。它是这样开始的:
Sub newton11()
Dim x As Double, z As Double, tolerance As Double
Dim error_x As Double, error_z As Double
Dim iteration As Integer
iteration = 0
tolerance = 0.05
x = Range("h19").value
z = Range("h20").value
however when I run the sub, it doesn't work. When I was debugging I noticed when I hovered over x it was assigned a value of -344 when Range("h19") is 53 and z was assigned -5.12 when Range("20") is 0.
但是当我运行sub时,它不起作用。当我调试时,我注意到当我在x上盘旋时,当Range(“h19”)为53时,它被赋值为-344,当Range(“20”)为0时,z被赋予-5.12。
does anybody know how to fix this?
有谁知道如何解决这个问题?
2 个解决方案
#1
6
Always define the sheet where you pull the data from. If you write:
始终定义从中提取数据的工作表。如果你写:
x = Range("H19").Value
by default you're saying:
默认你会说:
x = ActiveSheet.Range("H19").Value
which is probably containing the value -344
while you were waiting for 53
. With this:
当你等待53时,它可能包含值-344。这样:
x = Sheets("myGoodSheet").Range("H13").Value
you're sure you're referencing to the proper one. And as Mark says in his comment, even better if you reference the correct workbook with Workbooks(j)
just in front of the Sheets
collection.
你确定你正在引用正确的那个。正如Mark在他的评论中所说,如果你在Sheets集合前面使用Workbooks(j)引用正确的工作簿,那就更好了。
#2
2
What you're describing isn't possible.
你所描述的是不可能的。
Instead of this:
而不是这个:
x = Range("h19").value
z = Range("h20").value
Extract variables:
Dim xRange As Range, yRange As Range
Set xRange = Range("h19")
Set yRange = Range("h20")
x = xRange.Value
y = yRange.Value
Now place a breakpoint on the x = xRange.Value
line, and use the locals window (from the View
menu) to inspect the runtime value of xRange
and yRange
- then F8 and inspect the runtime value of x
: the two are the same.
现在在x = xRange.Value行上放置一个断点,并使用locals窗口(从View菜单中)检查xRange和yRange的运行时值 - 然后检查F8并检查x的运行时值:两者是相同的。
See @Matteo's answer for the why.
请参阅@ Matteo的答案。
#1
6
Always define the sheet where you pull the data from. If you write:
始终定义从中提取数据的工作表。如果你写:
x = Range("H19").Value
by default you're saying:
默认你会说:
x = ActiveSheet.Range("H19").Value
which is probably containing the value -344
while you were waiting for 53
. With this:
当你等待53时,它可能包含值-344。这样:
x = Sheets("myGoodSheet").Range("H13").Value
you're sure you're referencing to the proper one. And as Mark says in his comment, even better if you reference the correct workbook with Workbooks(j)
just in front of the Sheets
collection.
你确定你正在引用正确的那个。正如Mark在他的评论中所说,如果你在Sheets集合前面使用Workbooks(j)引用正确的工作簿,那就更好了。
#2
2
What you're describing isn't possible.
你所描述的是不可能的。
Instead of this:
而不是这个:
x = Range("h19").value
z = Range("h20").value
Extract variables:
Dim xRange As Range, yRange As Range
Set xRange = Range("h19")
Set yRange = Range("h20")
x = xRange.Value
y = yRange.Value
Now place a breakpoint on the x = xRange.Value
line, and use the locals window (from the View
menu) to inspect the runtime value of xRange
and yRange
- then F8 and inspect the runtime value of x
: the two are the same.
现在在x = xRange.Value行上放置一个断点,并使用locals窗口(从View菜单中)检查xRange和yRange的运行时值 - 然后检查F8并检查x的运行时值:两者是相同的。
See @Matteo's answer for the why.
请参阅@ Matteo的答案。