传递的参数与标准不匹配

时间:2014-04-17 08:56:11

标签: excel-vba argument-passing vba excel

我试图将参数从一个程序传递到另一个程序。一切看起来都不错,参数被传递,但它确实符合Select case场景中给出的标准。

我做错了什么?

传递参数的过程:

Private Sub but_next_Click()

Call zmien_fakture("but_next")

End Sub

接收参数的程序:

Private Sub zmien_fakture(ByVal txt_name As String)

Select Case txt_name

    Case txt_name = "but_next"
        If Lastrow > nr_faktury_dol Then
            nr_faktury_dol = nr_faktury_dol + 1

        Else
            MsgBox ("To jest ostatnia faktura")

        End If

    Case txt_name = "but_prev"
        If nr_faktury_dol <> 1 Then: nr_faktury_dol = nr_faktury_dol - 1

    Case Else

End Select

End Sub

1 个答案:

答案 0 :(得分:2)

Case txt_name = "but_next"替换为Case "but_next"。正确的代码是:

Private Sub zmien_fakture(ByVal txt_name As String)
    Select Case txt_name
        Case "but_next"
            If Lastrow > nr_faktury_dol Then
                nr_faktury_dol = nr_faktury_dol + 1
            Else
                MsgBox ("To jest ostatnia faktura")
            End If
        Case "but_prev"
            If nr_faktury_dol <> 1 Then: nr_faktury_dol = nr_faktury_dol - 1
        Case Else
    End Select
End Sub

为什么语法不起作用

当您使用Case txt_name = "but_next"时,语句txt_name = "but_next"会评估为TRUEFALSE,您会得到:

Select Case txt_name ' txt_name="but_next", but txt_name<>TRUE and txt_name<>FALSE
    Case True
        'do something
    Case False
        'do something
    Case Else
End Select

但在Select Case txt_name变量中txt_name不是TrueFalse,而您实际上是跳到Case Else部分。

替代方法是将Select Case txt_name更改为Select Case TRUE,如下所示:

Private Sub zmien_fakture(ByVal txt_name As String)
    Select Case True
        Case txt_name = "but_next"
            If Lastrow > nr_faktury_dol Then
                nr_faktury_dol = nr_faktury_dol + 1
            Else
                MsgBox ("To jest ostatnia faktura")
            End If
        Case txt_name = "but_prev"
            If nr_faktury_dol <> 1 Then: nr_faktury_dol = nr_faktury_dol - 1
        Case Else
    End Select
End Sub