Excel macro producing wrong results

I have inherited a macro written by another *gone* programmer which is not working as expected.  I am not experienced writing macros, but readingthis code and steppingthrough, seems to be correct and yet, it is not producing the expected results:

Sub spanseek()
    For cell = 5 To 27 Step 1

    If Range("I" + Format$(cell)) <> "" Then
            Range("I" + Format$(cell)).GoalSeek Goal:=0, ChangingCell:=Range("J" + Format$(cell))
    End If
    Next cell

End Sub

When I run this macro, rather than changing the column I cell to 0 and changing the J column to the correct figure, it leaves the I column as is and changes only the J column cell, but it seems to increment the J column every time it is run to a larger, and larger figure...I really don't know why it's is not wotking correctly as the code seems to be pretty straightforward.  Can anybody see something wrong with this?  Any feedback would be much appreciated.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

what exactly are you trying to do with the code?
presuming what you want to do is verify that the value of the I column is not empty, by the way you should check for this condition instead of the "" condition.  Since " " is not equivolent to "" in VB, make sure it is what you want it to be a number, blank, etc.

so my proposed code, If I understand the code is as follows:

for cell = 5 to 27 step 1

  if IsNumeric( Range("I" & cell).Value ) then

     Range("J" & cell).Value = cell ' this assumes you wants the value of cell in the J cell position
     'Range("J" & cell).Value = Range("I" & cell).Value ' this assumes you wants the value of I cell in the J cell position
     Range("I" & cell).Value = 0 'This sets the value to 0 for the I cell position

  end if

next cell

let me know if this helps

MosquitoeAuthor Commented:
Some good points - I should explain further:

Column I (lets say row 7 for example) is determined by subtracting G7-E7 (those two cells hold a bottle concentration)

Column G is detremined by this calculation: J7*J7^2+L7+J7^3*M7+J7^4+N7

which would look something like this if i used real values:
86.59 * 9.3558E-01 ^ 2 * 1.0350E-03 + 86.59 ^ 3 * - 8.5145E-06 + 86.59 ^ 4 + 3.5300E-08

So if column I is not 0, this means that the calculated bottle concentration and the actual bottle concentration are not the same value.  This also means that the calculated spam number is not correct which is column J.  So, the above macro is run and is supposed to calculate what the new spam number in column J would be if the value in column I is changed to match the value in column E.
har har har

I work with VBA all the time and often find that the devil is in the details.

I am a Chemical Engineer by education (concentrations speak to me !!!) and have been doing work for the Chemical/Petroleum Industry for the last 6 years.  I run across applications like this all the time.

I think I found your issue:

please change the "" to a 0 and rerun the code, because this is what you are really checking for in the G-E calculation, and get rid of all that Format stuff:

Private Sub spanseek_Click()

    For cell = 5 To 27 Step 1

   'make sure the difference between column G and E is actually a number if he/she did
   'intend to force a "" value, then I think this is wrong.  But you can put  back the "" if this is the case.

   'Checking to see if G-E is actually 0 please pay attention to precision ... ie is it REALLY ever zero
   'This should actually be something more like If abs(Range("I" & cell)) > 0.0001 or some such nonsense
   'Which speaks more to my point about getting rid of the "" check and going to a straight number evaluation.
   'Numbers should be used all over XLS, leave the text for reports.
   'note: if you keep the "" evaluation then get do not use the abs line above, since the function does not evaluate strings

    If Range("I" & cell) <> 0 Then

            'I guess I will say the same for the Goal: value here as well for precision.  But zero is a nice way

            Range("I" & cell).GoalSeek Goal:=0, ChangingCell:=Range("J" & cell)
    End If
    Next cell

End Sub

I ran this against the XLS macro that I created and it does not give an exact zero value more like +/- .0001 precision.  But XLS does report this as the value for the solution when I run it by hand, so the above calculation does work as I have configured it.  Oh, yeah the J column no longer increments.

let me know if you need further assistance.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MosquitoeAuthor Commented:

I ran the code with the revisions, with one alteration, there are some blank fields in the column so I had to filter for that condition.  But the result was what I was looking for - The devil IS in the details considering how minor these changes were - Thanks alot!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.