首页 > 解决方案 > 如何修复 VBA 中的“编译错误:未定义用户定义的类型”

问题描述

我目前正在使用大学的交易 API 设置交易算法。请记住,我对 VBA 相当陌生,但我只是对为什么我的代码在以下代码段中引发编译错误感到困惑:

我试过声明我的参数类型,但这也无济于事。正如您在代码段中看到的那样,我没有制作任何自定义类型。


Private Declare Sub AppSleep Lib “kerne132” Alias “Sleep” (ByVal dwMilliseconds As Long)
Public Sub Pause(PauseInSeconds As Long)
    Call AppSleep(PauseInSeconds * 1000)
End Sub


Function marketmake(time, triggerstart, triggerstop)
    ‘To initialize the API
DIm api As RIT2.API
Set API = New RIT2.API 
‘Run the algo during certain time frame in the simulation
If time < triggerstart And time > triggerstop Then
    ‘Check if any orders are backlogged, if not, then put in a bracket
    If Sheets(“Open Orders”).Cells(1, 2) = “” Then
        ‘The following loop submits the Buy section of bracket 
    Status = False
        Do While Status = False 
        Status = API.AddOrder(“ALGO”, Range(“Shares”), Range(“MidMarket”) - Range(“Spread”), API.SELL, API.LMT)
        Loop

‘The following loop submits the Sell section of bracket 
    Status = False
        Do While Status = False 
        Status = API.AddOrder(“ALGO”, Range(“Shares”), Range(“MidMarket”) - Range(“Spread”), API.BUY, API.LMT)
        Loop

ElseIf InStr(Sheets(“Open Orders”).Cells(2,1), “;” = 0 Then
    ‘Cancel all orders
    API.CancelOrderExpr (“price > 0”)
    End If 
    Marketmake = time + triggerstart
End If 
End Function



基本上我的算法是假设以设定的出价购买股票,然后在购买了必要的股票或循环持续时间达到任意运行时间时打破循环。

标签: excelvba

解决方案


你的 Instr 上需要一个右括号

ElseIf InStr(Sheets("Open Orders").Cells(2, 1), ";") = 0 Then

用正确的引号替换智能引号。

如果使用 64 位,还需要添加 PtrSafe

Private Declare PtrSafe Sub AppSleep Lib "kerne132" Alias "Sleep" (ByVal dwMilliseconds As Long)

和这个

Dim API As RIT2.API
Set API = New RIT2.API

依赖于你有一个类叫做RIT2


推荐阅读