Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA type mismatch in split function

Tags:

excel

vba

I have written a code where I need to get the number from a string like this: "3/1". I need to store those 2 numbers as integer in 2 different variables. I have written this code in 2 classes: This function is my split function in the class (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

In the main class I have a function calling to this function that splits the number to get the values that I want. However I am getting a "Type Mismatch error" I am not able to detect the reason of this type mismatch.

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

So, I am not able to see why it is not working correctly and I am getting the type mismatch error. Because, I believe that everywhere I am passing the same type.

like image 643
TMikonos Avatar asked Feb 05 '23 15:02

TMikonos


1 Answers

SplitValues returns a String array, and you are trying to assign it to a String. Try dimming values as String() instead.

You'll still have an issue when calling SplitValuesas you haven't created an instance of your class, just said that gen will be one. After Dim gen As cBigOne, you should have Set gen As New cBigOne

like image 161
bobajob Avatar answered Feb 14 '23 09:02

bobajob