Excel VBA编译错误:期望的子、函数或属性。

时间:2022-11-06 09:36:08

I am getting a compile error in Excel VBA which says Expected Sub, Function or Property. The function I am using is given below which is trying to copy the rate function in Excel. Thanks for your help.

我在Excel VBA中得到了一个编译错误,它表示预期的子函数或属性。下面给出了我正在使用的函数,它试图在Excel中复制速率函数。谢谢你的帮助。

Function rate_m(nper As Double, pmt As Double, pv As Double, fv As Double, types As Double, guess As Double) As Variant
    Dim y, y0, y1, x0, x1, f, i As Double
    Dim FINANCIAL_MAX_ITERATIONS As Double
    Dim FINANCIAL_PRECISION As Double

    If IsNull(guess) Then guess = 0.01
    If IsNull(fv) Then fv = 0   
    If IsNull(types) Then types = 0

    FINANCIAL_MAX_ITERATIONS = 128 'Bet accuracy with 128
    FINANCIAL_PRECISION = 0.0000001 '1.0e-8

    y , y0, y1, x0, x1, f, i = 0
    rate_m = guess

    If Abs(rate_m) < FINANCIAL_PRECISION Then
        y = pv * (1 + nper * rate_m) + pmt * (1 + rate_m * types) * nper + fv
    Else
        f = Exp(nper * Log(1 + rate_m))
        y = pv * f + pmt * (1 / rate_m + types) * (f - 1) + fv

        y0 = pv + pmt * nper + fv
        y1 = pv * f + pmt * (1 / rate_m + types) * (f - 1) + fv
    End If

    'find root by Newton secant method
    i , x0 = 0
    x1 = rate_m

    While Abs(y0 - y1) > FINANCIAL_PRECISION & i < FINANCIAL_MAX_ITERATIONS
        rate_m = (y1 * x0 - y0 * x1) / (y1 - y0)
        x0 = x1
        x1 = rate_m

        If Abs(rate_m) < FINANCIAL_PRECISION Then
            y = pv * (1 + nper * rate_m) + pmt * (1 + rate_m * types) * nper + fv
        Else
            f = Exp(nper * Log(1 + rate_m))
            y = pv * f + pmt * (1 / rate_m + types) * (f - 1) + fv
        End If

        y0 = y1
        y1 = y
        i = i + 1
    Wend
End Function

2 个解决方案

#1


1  

Every IF ends with a End If (unless in a single line) and While...loop. You might want to take a look at VBA's syntax:
http://msdn.microsoft.com/en-us/library/office/ee814737(v=office.14).aspx

如果(除非在一行中)和While循环,每个IF都结束。您可能想看看VBA的语法:http://msdn.microsoft.com/en-us/library/office/ee814737(v=office.14).aspx。

EDIT: You have to declare variable individually, instead of: y , y0, y1, x0, x1, f, i = 0

编辑:你必须单独声明变量,而不是:y, y0, y1, x0, x1, f, i = 0。

you could do:
y = 0
y0 = 0
y1 = 0
x0 = 0
x1 = 0
f = 0
i = 0

你可以这样做:y = 0 y = 0 y = 0 y = 0 x = 0 f = 0, i = 0。

#2


2  

A couple things...

一些事情……

First, you have to assign each variable individually...like this:

首先,你必须分别分配每个变量…是这样的:

y = 0
y0 = 0
y1 = 0
x0 = 0
x1 = 0
f = 0
i = 0

Second, you probably want to DIM your variables all as Double. Unfortunately, this line:

第二,你可能想要把你的变量都变模糊。不幸的是,这条线:

Dim y, y0, y1, x0, x1, f, i As Double

Only declares i as a Double, all the others will be a Variant. You need to declare each one individually, like this:

只声明i为Double,所有其他的都将是一个变体。您需要单独声明每一个,如下所示:

Dim y As Double
Dim y0 As Double
Dim y1 As Double
Dim x0 As Double
Dim x1 As Double
Dim f As Double
Dim i As Double

#1


1  

Every IF ends with a End If (unless in a single line) and While...loop. You might want to take a look at VBA's syntax:
http://msdn.microsoft.com/en-us/library/office/ee814737(v=office.14).aspx

如果(除非在一行中)和While循环,每个IF都结束。您可能想看看VBA的语法:http://msdn.microsoft.com/en-us/library/office/ee814737(v=office.14).aspx。

EDIT: You have to declare variable individually, instead of: y , y0, y1, x0, x1, f, i = 0

编辑:你必须单独声明变量,而不是:y, y0, y1, x0, x1, f, i = 0。

you could do:
y = 0
y0 = 0
y1 = 0
x0 = 0
x1 = 0
f = 0
i = 0

你可以这样做:y = 0 y = 0 y = 0 y = 0 x = 0 f = 0, i = 0。

#2


2  

A couple things...

一些事情……

First, you have to assign each variable individually...like this:

首先,你必须分别分配每个变量…是这样的:

y = 0
y0 = 0
y1 = 0
x0 = 0
x1 = 0
f = 0
i = 0

Second, you probably want to DIM your variables all as Double. Unfortunately, this line:

第二,你可能想要把你的变量都变模糊。不幸的是,这条线:

Dim y, y0, y1, x0, x1, f, i As Double

Only declares i as a Double, all the others will be a Variant. You need to declare each one individually, like this:

只声明i为Double,所有其他的都将是一个变体。您需要单独声明每一个,如下所示:

Dim y As Double
Dim y0 As Double
Dim y1 As Double
Dim x0 As Double
Dim x1 As Double
Dim f As Double
Dim i As Double