split 函数中的 VBA 类型不匹配

标签 vba excel

我编写了一段代码,需要从如下字符串中获取数字:“3/1”。我需要将这两个数字作为整数存储在两个不同的变量中。我在两个类中编写了这段代码: 这个函数是我在类(BigOne)中的分割函数

Public Function SplitValues(pInput As String, pdelim As String) As String()
'Declaration of variables
Dim strSplit() As String
Dim countDelim As Integer

'Initialization of variables

countDelim = countCharacter(pInput, pdelim)
If countDelim > 0 Then
    ReDim strSplit(countDelim)
    strSplit = Split(pInput, pdelim)
    SplitValues = strSplit
End If
End Function

在主类中,我有一个函数调用该函数来分割数字以获得我想要的值。但是,我收到“类型不匹配错误”,我无法检测到这种类型不匹配的原因。

Public Function get_MaxChars(pInput As String) As Integer
'declaration of variables
    Dim gen As cBigOne
    Dim values As String

   'Main code
    pInput = CStr(pInput)
    Debug.Print (pInput)
    values = gen.SplitValues(pInput, "/")
    get_MaxChars = CInt(values(0))

End Function

所以,我不明白为什么它不能正常工作,并且出现类型不匹配错误。因为,我相信我在任何地方都传递相同的类型。

最佳答案

SplitValues 返回一个字符串数组,并且您正尝试将其分配给一个字符串。尝试将 values 调暗为 String()

调用 SplitValues 时仍然会遇到问题,因为您尚未创建类的实例,只是说 gen 将是一个。在 Dim gen As cBigOne 之后,您应该有 Set gen As New cBigOne

关于split 函数中的 VBA 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40842732/

相关文章:

excel - 为什么 Excel 有时会产生 'incorrect' 错误消息?

python - 尝试使用 Python 从 excel 电子表格中删除第一行

excel - VBA 编译器不允许在方法参数周围使用括号

VBA 如何在按下表单按钮时强制函数返回

vba - 如何使用 Word VBA 删除命令按钮?

sql - OLE DB 提供程序 'Microsoft.Jet.OLEDB.4.0' 无法用于分布式查询

excel从模式平均值中排除某个值

vb.net - vb.net中windows应用的全局化

excel - 删除 'NULL' 值的代码

vba - 范围已变成表格,宏将无法运行