类型不匹配错误:将类public get属性作为参数传递给子过程

时间:2014-10-07 00:25:24

标签: vba excel-vba excel

我试图传递给代码相同的单独的类属性,除了它们指向具有两个参数的子过程的两个不同的单元格。

我遇到类型不匹配错误,但无法弄清楚它为什么会发生......你不能将类属性传递给子程序吗?

班级代码

Public Property Get Signal() ' this code is in the EURO_USD class

Dim rowA_status As String
rowA_status = Worksheets("EURO_USD").Cells(rows.Count, "E").End(xlUp).Value

Select Case rowA_status
    Case Not ""
        Signal = rowA_status
        ActiveWorkbook.CustomDocumentProperties.Item("State_euro_usd").Value = rowA_status
    Case ""
        Signal = "no news"

End Select

End Property

Public Property Get Signal() As String 'This code is in the DNB_EMA class NOT EURO_USD class

Dim rowA_status As String
rowA_status = Worksheets("DNB_EMA").Cells(rows.Count, "E").End(xlUp)

Select Case rowA_status.Value
    Case Not ""
        Signal = rowA_status
        ActiveWorkbook.CustomDocumentProperties.Item("").Value = rowA_status
    Case ""
        Signal = "no news"

End Select

End Property

其他代码

Private sub run() ' start the email_handler
Dim email_handler As Object: Set email_handler = New email_handler

email_handler.Send EURO_USD.Signal, DNB_EMA.Signal <---- this generates type mismatch error!

End sub

Sub Send(Action_EURO_USD As String, Action_DNB_EMA As String) ' send news

Set OutApp = New Outlook.Application: Set outMail = OutApp.CreateItem(olMailItem)

With outMail
    .To = ""
    .Subject = "Bot_Trading_Status"
    .body = "Status for EURO_USD: " & Action_EURO_USD & vbCrLf & "Status for DNB_EMA: " & Action_DNB_EMA
    .Send
    End With
End Sub

2 个答案:

答案 0 :(得分:1)

好的,所以我创建了一个普通模块"main"并将其放入:

Sub foo()

Dim euro As myClass

'instantiate the class object
Set euro = New myClass

Call email_handler.Send(euro.Signal, "blah")

End Sub

您的类模块代码实际上已经破解,它在.Send过程的函数调用中不是不匹配,但是它无法评估Signal属性,并且错误冒泡到调用程序。

出现错误是因为此行正在尝试对字符串数据类型进行布尔评估,是错误匹配错误的来源。

Case  Not ""

我修改了名为"myClass"的类模块,在我的示例中,包含此代码。您仍然可以使用Case语句,但更改方式如下:

Public Property Get Signal() As String

Dim rowA_status As String
rowA_status = Worksheets("EURO_USD").Cells(Rows.Count, "E").End(xlUp).Value

Select Case rowA_status
    Case vbNullString
        Signal = "no news"
    Case Else
        'equivalent to: NOT ""

        Signal = rowA_status
        ActiveWorkbook.CustomDocumentProperties.Item("State_euro_usd").Value = rowA_status

End Select

Signal = rowA_status

End Property

答案 1 :(得分:0)

select case会导致错误,将其更改为If语句可以解决问题。但是我不知道为什么会这样。