函数返回0

时间:2018-12-18 14:06:21

标签: excel vba function variables

我正在编写文件导入过程。我将列号作为函数映射到两个文件之间

我传入一个名为c的变量,该变量是导入文件上的列,然后我在一个函数中使用case select来输出具有映射列号的另一个变量名(我可能做错了

大小写选择的工作原理是,它在正确的大小写处停止,经过cNew = x,然后跳转到函数的末尾

我遇到的问题是,新变量始终返回0。变量和函数会变暗为整数

For c = 2 To 19
cNew = cMapped(c)
MsgBox (cNew)
Next c

Function cMapped(c As Integer) As Long
Select Case c
    Case Is = 1
    cNew = 1
    Case Is = 2
    cNew = 3
    Case Is = 3
    cNew = 2
    Case Is = 4
    cNew = 7
    Case Is = 5
    cNew = 5
    Case Is = 6
    cNew = 16
    Case Is = 7
    cNew = 19
    Case Is = 8
    cNew = 21
    Case Is = 9
    cNew = 27
    Case Is = 10
    cNew = 30
    Case Is = 11
    cNew = 6
    Case Is = 12
    cNew = 11
    Case Is = 13
    cNew = 10
    Case Is = 14
    cNew = 32
    Case Is = 15
    cNew = 28
    Case Is = 16
    cNew = 33
    Case Is = 17
    cNew = 99
    Case Is = 18
    cNew = 50
    Case Is = 19
    cNew = 8
End Select

End Function

3 个答案:

答案 0 :(得分:1)

请尝试这个。您必须以这种方式返回值。而且我认为select在VBA中看起来是这样的。

Function cMapped(c As Integer) As Long

Select Case c
    Case 1:
    cNew = 1
    Case 2:
    cNew = 3

End Select
cMapped = cNew

End Function

答案 1 :(得分:0)

Select Case c
  Case 1
    cNew = 1
  Case 2
    cNew = 2
  ...
return cNew
...

这样做更好吗?

答案 2 :(得分:0)

根据user10798192和Michael Puckett II的回答,很明显我没有正确返回任何东西

这是调整后的代码;

   For c = 2 To 19
     cNew = cMapped(c)
     MsgBox (cNew) 
     Next c

以及修改后的功能

Function cMapped(c As Integer) As Long
Select Case c
    Case Is = 1
    cMapped = 1
    Case Is = 2
    cMapped = 3
    Case Is = 3
    cMapped = 2
    Case Is = 4
    cMapped = 7
    Case Is = 5
    cMapped = 5
    Case Is = 6
    cMapped = 16
    Case Is = 7
    cMapped = 19
    Case Is = 8
    cMapped = 21
    Case Is = 9
    cMapped = 27
    Case Is = 10
    cMapped = 30
    Case Is = 11
    cMapped = 6
    Case Is = 12
    cMapped = 11
    Case Is = 13
    cMapped = 10
    Case Is = 14
    cMapped = 32
    Case Is = 15
    cMapped = 28
    Case Is = 16
    cMapped = 33
    Case Is = 17
    cMapped = 99
    Case Is = 18
    cMapped = 50
    Case Is = 19
    cMapped = 8
End Select
End Function