Excel Visual Basic Issue

1

I've been using VBA quite a bit recently, and, so far, I haven't run into any issues. For some reason, and, I cannot for the life of me figure it out, visual basic wont include "+" or "-" signs in subroutines. The following is what I want the program to do:

Function f(x As Single) As Single

    f = 0.2 + 25 * x - 200 * x ^ 2 + 675 * x ^ 3 - 900 * x ^ 4 + 400 * x ^ 5

End Function

Sub Simp()

Dim x As Single, y As Single

n = Cells(3, 3)
a = Cells(4, 3)
b = Cells(5, 3)

For i = 1 To n

    a + i = Cells(2 + i, 1)


Next i

End Sub

The problem I've been having is that whenever I type in "a+i=Cells(2+i,1)" the program changes it to "a i=Cells(2+i,1)" and says "Compiling Error: Expected Sub, Function or Property." The same thing is not happening to other, completed, programs. What is this and how do I fix it?

user11781

Posted 2014-04-21T06:38:16.150

Reputation: 13

3Have you tried swapping the equation? Put the Cells(...) on the left side of the equals sign. You cannot assign a value to "a + i" – TheUser1024 – 2014-04-21T11:55:30.933

Answers

0

You mention -

The problem I've been having is that whenever I type in "a+i=Cells(2+i,1)" the program changes it to "a i=Cells(2+i,1)" and says "Compiling Error: Expected Sub, Function or Property."

The editor is expecting a to be a call to another subroutine or function as you're not doing anything with the a after it converts it to a i=cells(2+i,1). The conversion happens because you're not telling it to do anything prior to calculating a+i and therefore it won't perform the operation.

To correct this you'll need to specify a location to store the result of a+i prior to the calculation. Do this by reversing the order of your operation:

Cells(2+i,1)=a+i

Raystafarian

Posted 2014-04-21T06:38:16.150

Reputation: 20 384