【问题标题】:Excel vba split function not workingExcel vba拆分功能不起作用
【发布时间】:2017-10-25 16:25:16
【问题描述】:

我试图在一个字符串中出现多个字符。它应该工作,但它没有。

Function countLetter(letter As String, secretWord As String)
    MsgBox (Split(secretWord, letter).Length)
    countLetter = Split(secretWord, letter).Length - 1
End Function

这里有什么问题?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    Split 没有.Length 属性。使用UBound 对数组中的元素进行从零开始的计数。

    Function countLetter(letter As String, secretWord As String)
        MsgBox UBound(Split(secretWord, letter)) + 1
        countLetter = UBound(Split(secretWord, letter))
    End Function
    

    【讨论】:

    • @Ans 与拆分无关,但请注意传递给 MsgBox 函数的 Message 参数周围缺少括号;每当你调用一个过程而不将它的返回值捕获到一个局部变量中时,你不应该在参数周围加上括号。这一天或一天​​给你带来麻烦。
    猜你喜欢
    • 1970-01-01
    • 2014-03-05
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 2018-09-24
    • 1970-01-01
    • 2017-03-24
    相关资源
    最近更新 更多