当前位置:编程学习 > VB >>

求vba高手指点!!

本人自己写的一段vba代码,如下:
类:
Option Explicit
Private real_num As Double
'实部
Private imaginary_num As Double
'虚部
Public Property Get real() As Double
real = real_num
End Property

Public Property Let real(ByVal re As Double)
real_num = re
End Property

Public Property Get imaginary() As Double
imaginary = imaginary_num
End Property

Public Property Let imaginary(ByVal imag As Double)
imaginary_num = imag
End Property

Public Sub sum(a As Complex_numble, b As Complex_numble)
real = (a.real + b.real)
imaginary = (a.imaginary + b.imaginary)
End Sub
'复数加法

Public Sub subtract(a As Complex_numble, b As Complex_numble)
real = (a.real - b.real)
imaginary = (a.imaginary - b.imaginary)
End Sub
'复数减法

Public Sub multiply(a As Complex_numble, b As Complex_numble)
real = (a.real * b.real - a.imaginary * b.imaginary)
imaginary = (a.real * b.imaginary + a.imaginary * b.real)
End Sub
'复数乘法

Public Sub divide(a As Complex_numble, b As Complex_numble)
real = ((a.real * b.real + a.imaginary * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary))
imaginary = ((b.real * a.imaginary - a.real * b.imaginary) / (b.real * b.real + b.imaginary * b.imaginary))
End Sub
'复数除法

Public Sub sin(a As Complex_numble)
sin.real = ((Math.sin(a.real) * (Math.Exp(a.imaginary) - Math.Exp(-a.imaginary))) / 2)
sin.imaginary = ((Math.cos(a.real) * (Math.Exp(a.imaginary) - Math.Exp(-a.imaginary))) / 2)
End Sub
'正弦
Public Sub cos(a As Complex_numble)
real = ((Math.cos(a.real) * (Math.Exp(a.imaginary) + Math.Exp(-a.imaginary))) / 2)
imaginary = ((Math.sin(a.real) * (Math.Exp(-a.imaginary) - Math.Exp(a.imaginary))) / 2)
End Sub
'余弦
Private Sub Class_Initialize()
real = 1
imaginary = 1
End Sub
模块:
Attribute VB_Name = "模块2"
Public Sub text1()
Dim a As New Complex_numble
Dim b As New Complex_numble
a.real = CDbl(Range("a2"))
a.imaginary = CDbl(Range("b2"))
b.real = CDbl(Range("a3"))
b.imaginary = CDbl(Range("b3"))
b.sin (a)
Range("c2") = b.real
Range("d2") = b.imaginary
End Sub
在这个标红的地方运行不下去了,请高手指点哪里错了 vba 类 --------------------编程问答-------------------- --------------------编程问答-------------------- 要改为: Call b.sin(a)
  或者: b.sin a
(就是把那个括号去掉)

另外,你的 Public Sub sin(a As Complex_numble) 里面,那两个 sin. 都要去掉。
--------------------编程问答-------------------- 多谢明月指点,问题已解决! --------------------编程问答-------------------- 追问一下,如果我想把类中的所有数学运算方法改写为function函数,该如何实现外部调用? --------------------编程问答-------------------- 这个很简单呀。
如果改成函数调用,这个就没必要用类模块了。
把你的那些所有的 Sub 移到一个标准模块中就行。
然后你参考这个代做:
Option Explicit

' 定义复数数据类型
Public Type Complex_numble
' 如果要在别的模块中使用这种类型,就不要用 Private !
' 在标准模块代码中, Public 是默认的,可以不写。
   real      As Double
   imaginary As Double
End Type

' 下面就是一些“运算”函数的实现…………

'复数加法
Public Function sum(a As Complex_numble, b As Complex_numble) As Complex_numble
   sum.real = a.real + b.real
   sum.imaginary = a.imaginary + b.imaginary
End Function

' ***** 其它的函数,你照着改 *****
'注意:如果“运算”需要两个复数来做,那你的函数参数就必须要有两个。


再给你一个调用的示例:
Private Sub Test()
   Dim a As Complex_numble
   Dim b As Complex_numble
   Dim c As Complex_numble
   
   a.real = 5
   a.imaginary = 20
   b.real = 16
   b.imaginary = -30
   c = sum(a, b)
   Debug.Print c.real, c.imaginary
' 输出:
' 21           -10 
End Sub
--------------------编程问答-------------------- 代码写到标准模块中,那几个 Public  Property  Get / Let 的,直接就不要了。
--------------------编程问答-------------------- 呵呵,我搞复杂了
补充:VB ,  VBA
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,