简体   繁体   中英

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. I have an equation that I'm putting a range of values for i into, and attempting to retrieve values of 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.:

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". 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. 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. 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?

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. 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". 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.

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). 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?

As an aside, writing single cells values to excel is very unlikely to be efficient. A lot quicker will be writing everything into an array and then writing the array to 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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