gpt4 book ai didi

vba - 使用在类中创建的函数 - VBA

转载 作者:行者123 更新时间:2023-12-03 02:38:24 25 4
gpt4 key购买 nike

我在类里面学习如何工作。请帮助我如何做这样的事情。

我在 Excel 中准备了该类:

类名“Complex”

Option Explicit

Public Re As Double 'Real
Public Im As Double 'Imaginary

Public Function CCAdd(ParamArray Complex1() As Variant) as Complex
Dim i As Variant
Dim ZXC As Complex
Set ZXC = New Complex

For i = 0 To UBound(Complex1) 'Numer of arguments passed to the function
If Not IsMissing(Complex1(i)) Then 'Omit arguments which don't exist
If TypeName(Complex1(i)) = "Complex" Then ' Check is it Complex
ZXC.Re = Complex1(i).Re + ZXC.Re
ZXC.Im = Complex1(i).Im + ZXC.Im
End If
End If
Next i
Set Add = ZXC
Set ZXC = Nothing
End Function

在我放置的模块中:

sub asd()
Dim K As Complex
Dim Z As Complex
Dim A As Complex

Set K = New Complex
Set Z = New Complex
Set A = New Complex

K.Re = 1
K.Im = 2

Z.Re = 3
Z.Im = 4

A = K.CCAdd(K, Z)
end sub

执行函数 CCAdd 后,将值传递给“A”时出现错误。怎么解决这个问题?也许函数无法传递非标准类型的数据。

最佳答案

在 Complex 类中,这一行:

Set Add = ZXC

需要:

Set CCAdd = ZXC

然后在模块中,您只需要:

Sub asd()
Dim K As Complex
Dim Z As Complex
Dim A As Complex

Set K = New Complex
Set Z = New Complex

K.Re = 1
K.Im = 2

Z.Re = 3
Z.Im = 4

Set A = K.CCAdd(K, Z)
End Sub

所以你不需要使用Set A = New Complex但你确实需要使用 Set将其分配给 CCAdd 的结果时功能。

关于vba - 使用在类中创建的函数 - VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34948596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com