在简单的VBA表单和命令按钮单击代码上编译错误

时间:2019-06-20 21:40:51

标签: excel vba compiler-errors

当我选择一个单选按钮并在表单上单击“确定”时,仅出现代码的第一行,并且宏标题以黄色突出显示时,出现Compile Error: Expected function or variable错误。我的表单实际上是4个单选按钮和一个ok按钮,下面的代码应该从中退出。

我已经检查了宏名称和选项按钮名称

Private Sub CommandButton1_Click()

    If Ties.Radio_OpPlan.Value = True Then
        Run clearties

    ElseIf Ties.Radio_Prior.Value = True Then
        Run TieToPrevious

    ElseIf Ties.Radio_Custom.Value = True Then
        Run CustomTie

    ElseIf Ties.Radio_NetCase.Value = True Then
        Run NetCaseTie

    Else
        output = MsgBox("You need to pick a case to tie to", vbExclamation)

    End If

    Me.Hide
    output = MsgBox("Case tied out", vbOKOnly)

End Sub

2 个答案:

答案 0 :(得分:5)

由于使用了Run,您的问题不止于此。用于从宏名称中将其作为字符串来调用。

尝试从其他方法中删除Run,看看是否可以解决。

Private Sub CommandButton1_Click()

    If Ties.Radio_OpPlan.Value = True Then
        clearties

    ElseIf Ties.Radio_Prior.Value = True Then
        TieToPrevious

    ElseIf Ties.Radio_Custom.Value = True Then
        CustomTie

    ElseIf Ties.Radio_NetCase.Value = True Then
        NetCaseTie

    Else
        output = MsgBox("You need to pick a case to tie to", vbExclamation)
    End If

    Me.Hide
    output = MsgBox("Case tied out", vbOKOnly)

End Sub

附加说明

  • Try not to use underscore case。下划线在VBA中对于事件和实现具有特殊含义。因此,除了Radio_OpPlan之外,您还可以执行RadioOpPlan,甚至可以简单地使用OpPlan(实际上甚至可以使OpPlan更具描述性)。

  • 在我看来,在这种情况下,Case语句看起来更干净。

Select Case True

    Case RadioOpPlan
        clearties

    Case RadioPrior
        TieToPrevious

    Case RadioCustom
        CustomTie

    Case RadioNetCase
        NetCaseTie

    Case Else
        output = MsgBox("You need to pick a case to tie to", vbExclamation)

End Select

答案 1 :(得分:0)

这是有效的方法,所有宏都必须确保它们从选择我首先要进行更改的工作表开始

$ORACLE_HOME/sqlplus -S 'username/password@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS= 
(COMMUNITY=tcp.world)(PROTOCOL=TCP)(Host=umyhostname)(Port=1599))) 
(CONNECT_DATA=(SID=idb)))' @/usr/local/myrpt.sql
相关问题