我编写了一段代码,需要从如下字符串中获取数字:“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/