(Solved) – VBA – Function is called depending on its position in code


I am trying to create an excel function which will fill column with data gathered from OGame’s reports. I have encountered strage behavior (at least from my point of view).
I have a form where the user can copy their reports to a form’s textbox. After clicking the button, the following functions should start:

Option Explicit

Private Sub CommandButton1_Click()
    Dim arr() As String
    Dim test As Boolean
    Dim str As String

    str = testFunction()
    arr = readData(Raporty.Value)

'    Dim element As Variant
'    For Each element In arr
'        Debug.Print element
'        Debug.Print "-------------------------------"
'    Next element

    test = writeData(arr)
    Debug.Print (test)
    Debug.Print ("str: "   str)

    Unload ufWczytajRaporty
End Sub

Private Function testFunction() As String
    Debug.Print ("testFunction")
    testFunction = "testFunctionText"
End Function

Private Function writeData(arr1() As String) As Boolean
    Debug.Print ("writeData")

    For Each element In arr1
        writeDataRow (element)
    Next element

    writeData = True
End Function

Private Function readData(text As String) As String()
    Debug.Print ("readData")

    Dim list As Object
    Dim substr As Integer
    Dim temp As String
    Dim arr(0 To 10) As String
    Dim result() As String
    Dim counter As Integer
    Set list = CreateObject("System.Collections.ArrayList")
    counter = 0

    If text = "" Then
        MsgBox "Dane nie mogą być puste! Proszę uzupełnić poprawnymi raportami!", vbCritical
        If InStr(1, text, "Przybył statek handlowy") = 1 Then
            While (InStr(1, text, "Towar został już odebrany."))
                substr = InStr(1, text, "Towar został już odebrany.")   26
                arr(counter) = Left(text, substr)
                temp = Mid(text, substr, Len(text))
                text = temp
                counter = counter   1
            Unload ufWczytajRaporty
            MsgBox "Niepoprawne raporty! Proszę wkleć dobre raporty!", vbCritical
        End If
    End If

    ReDim result(0 To counter)
    Dim i As Integer
    For i = 0 To counter
        result(i) = arr(i)
    Next i

    readData = arr
End Function

This functions provide the following result in Immediate:

 str: testFunctionText

As you can see the above false is a call of writeData fuction, however it never entered the function. writeData function contains Debug.Print (“writeData”) but it was never displayed.
Furthermore if I change places the following function:

str = testFunction()
arr = readData(Raporty.Value)


arr = readData(Raporty.Value)
str = testFunction()

testFunction is not called aswell.

Can someone explain me what is happening? Why those functions are not called?

Leave a Reply

Your email address will not be published. Required fields are marked *