-2

我是 vba 的新手,所以请原谅任何词汇失礼。我的函数是一个非常基本的 if 语句。调试时运行良好,但是当我从查询中调用它时,我得到了可怕的“查询中未定义的函数”错误。这是我从查询中调用它的方式: color2: ItemColor([Color])。目的是取一个库存ID,根据ID码拉出颜色。这个函数最终会针对大量颜色(超过7种)进行扩展,所以我不能在查询中使用iif函数。我想先用几个颜色选项来测试 vba。

这是我的代码:

Public Sub ItemColor()

If [Color] = "01" Then
    MsgBox "natural"

  Else
     MsgBox "shell"
  End If
End Sub

[颜色] 是同一查询中的一个字段。[Color] 是使用 Mid 函数从 ID 字段中提取的字符串。这个 vba 函数可以在这样的领域工作吗?

可能是因为我使用的是 msgbox 吗?我尝试使用 Dim,(请参阅下面的代码)但它仍然无法正常工作。也许我的语法错误?

Public Sub ItemColor()
Dim appearance As String

If [Color] = "01" Then
     appearance "natural"

  Else
     appearance "shell"
  End If
End Sub
4

1 回答 1

1

首先,您需要一个函数来返回一个值,因此错误状态为未定义函数。如果您想根据提供的数字对返回值进行硬编码,请尝试以下代码:

Public function getColor(colorID) as string
    Dim strColor as string

    Select case colorID
        Case 1
            StrColor = "color name"
        Case 2
            Strcolor = "different color"
    End select
    Getcolor = strcolor
End function
于 2018-06-04T03:46:50.237 回答