代码之家  ›  专栏  ›  技术社区  ›  Viktor Ilienko

VBA,自定义项中没有引号的字符串参数-如何访问它们的值?

  •  0
  • Viktor Ilienko  · 技术社区  · 6 年前

    我在VBA中的功能是:

    Function myFunc(a)
        myFunc = a    
    End Function
    

    当我在Excel工作表中这样使用这个函数时 =myFunc("abc") =myFunc(abc) ,则我收到错误消息 #NAME? .

    Function myFunc(a) Function myFunc(chr(34) & a & chr (34) ) 导致错误 Expected: ) .

    更新:我需要它来简化用户自定义项的使用。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Rafał B.    6 年前

    我不知道你为什么需要这样的东西。 但这是可能的 ! 了解应用程序调用方-它是运行UDF的rng。

     Private Function myFuncCalc(ByVal xstr As String)
     ' it is your main function to calculate what you want
     ' just sample code to test below
       If xstr = "USD" Then
        myFuncCalc = "yes it's american dollar!"
       Else
        myFuncCalc = "it's no american dollar"
       End If
     End Function
    
     Function myFunc(a)
     ' function just to be available in worksheet
     ' and extracting currency letter codes from formula between brackets
       bra1 = InStr(Application.Caller.Formula, "(")
       bra2 = InStr(Application.Caller.Formula, ")")
       x = Mid(Application.Caller.Formula, bra1 + 1, bra2 - bra1 - 1)
       myFunc = myFuncCalc(x)
     End Function
    

    瞧!

        2
  •  1
  •   Sama Tori    6 年前

    myfunction(a as Range) a.Value2 a.Text .