简体   繁体   English

为什么这个循环不能正常工作? 我一直收到错误对象'_Global'的方法'范围'失败

[英]Why isn't this loop working correctly? I keep getting the error Method 'Range' of object '_Global' failed

I'm attempting to graph lnD and i, where i is the x-axis, and lnD is the y-axis. 我试图绘制lnD和i,其中i是x轴,lnD是y轴。 I have an equation that I'm putting a range of values for i into, and attempting to retrieve values of lnD. 我有一个等式,我正在为i输入一系列值,并尝试检索lnD的值。

However, I came across an odd issue. 但是,我遇到了一个奇怪的问题。 First off, here is the code. 首先,这是代码。 I should note that it causes my Excel to freeze up for a few seconds, but it doesn't crash or anything.: 我应该注意它会导致我的Excel冻结几秒钟,但它不会崩溃或任何事情:

Tracker = 0
Alpha = -1.593975
Beta = -334.6942

For i = 0 To 0.1 Step 0.01
    Tracker = Tracker + 1
    lnD = Beta * i + Alpha
    Range("XFB" & Tracker).Value = i
    Range("XFC" & Tracker).Value = lnD
Next i

I get the error "Method 'Range' of object '_Global' failed". 我得到对象'_Global'失败的错误“方法'范围'”。 And when I look at the columns where the data should be, it is just i = 0 and lnD = -1.593975, repeating over and over again. 当我查看数据所在的列时,它只是i = 0和lnD = -1.593975,一遍又一遍地重复。 When I look at the value of Tracker, it has increased into the 10 thousands, and since all of the columns are full to the bottom of excel, that means the loop is actually looping. 当我查看Tracker的值时,它已经增加到10万,并且因为所有列都已经填满了excel的底部,这意味着循环实际上是循环的。 But why is i getting stuck at zero, and not increasing? 但为什么我会陷入零,而不是增加? Why am I getting this error? 为什么我收到此错误?

EDIT: I should note that if you change the top line to For i = 0 to 10 step 1, it works... So does this have to do with the numbers I'm putting in? 编辑:我应该注意,如果你将顶线更改为For i = 0到10 step 1,它可以工作......那么这与我输入的数字有关吗?

EDIT 2: So, after getting advice that it's an error not present in the code I put here, I looked into my variable declarations. 编辑2:所以,在获得建议后,我在这里放置的代码中没有出现错误,我查看了我的变量声明。 The issue ended up being that I declared i as an integer! 问题最终是我宣布我是一个整数! That made it round down to zero, causing the loop to get stuck at i = 0, and never making it to a "stopping point". 这使得它向下舍入为零,导致循环卡在i = 0,并且从未使其成为“停止点”。 Just a silly mistake! 只是一个愚蠢的错误!

I'm not going to delete this post, only because I feel like I should put my stupidity on display. 我不会删除这篇文章,只是因为我觉得我应该把我的愚蠢显示出来。 Thank you for helping, everyone! 感谢大家的帮助!

Try the following. 请尝试以下方法。 Rather than moving the target range, I always find it better to fix on the top of my trget and use offset to populate the cells bwllow and to the right. 而不是移动目标范围,我总是发现更好地固定在我的trget顶部并使用偏移来填充bwllow和右边的单元格。

Sub testit()

Dim StartCell As Range
tracker = 0
Alpha = -1.593975
Beta = -334.6942

Set StartCell = ActiveSheet.Range("XFB1")

For i = 0 To 0.1 Step 0.01
    lnD = Beta * i + Alpha
    StartCell.Offset(tracker, 0).Value = i
    StartCell.Offset(tracker, 1).Value = lnD
    tracker = tracker + 1
Next i

End Sub

Your code does work on my PC (after changing the columns to "A" and "B" as I'm working in Excel 2010 and my columsn don't run up all the way to where you're writing to). 您的代码可以在我的PC上运行(将列更改为“A”和“B”,因为我在Excel 2010中工作,而我的columsn不会一直运行到您写入的位置)。 Your comments seem to be indicating that your actually looping over a lot more values for i than you're stating ("Tracker is in the 10,000s while i only goes through 10 steps), can something there cause a problem? 你的评论似乎表明,你实际上已经为你传递了更多的值,而不是你所说的(“跟踪器在10,000s而我只经历了10步),那会有什么问题吗?

As an aside, writing single cells values to excel is very unlikely to be efficient. 另外,将单个单元格值写入excel是不太可能有效的。 A lot quicker will be writing everything into an array and then writing the array to excel. 将所有内容写入数组然后将数组写入excel会更快。

What works on my excel is (please note that I change the start of the output to "A1", be careful you don't overwrite any data): 在我的excel上有效的是(请注意我将输出的开头更改为“A1”,小心不要覆盖任何数据):

Sub test()

   ' Parameters
   Dim Alpha#:         Alpha = -1.593975
   Dim Beta#:          Beta = -334.6942
   Dim nmbOfSteps&:    nmbOfSteps = 11&
   Dim increment#:     increment = 0.01
   Dim startValue#:    startValue = 0#

   ' Fill in values in an array
   Dim result() As Double, cntr&
   ReDim result(1 To nmbOfSteps, 1 To 2)
   For cntr = 1 To nmbOfSteps
      Dim iValue#: iValue = startValue + CDbl(cntr - 1&) * increment
      result(cntr, 1) = iValue
      result(cntr, 2) = Alpha + Beta * iValue
   Next cntr

   ' Write the entire array in one go
   ThisWorkbook.ActiveSheet.Range("A1").Resize(nmbOfSteps, 2).Value2 = result

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM