mercredi 22 avril 2015

when code is executed execution won't move inside a excel vba function

For VBA functions and most programming in general i understand general program execution to occur something like this...

Inside a sub procedure if you call a function (it should return a value), in this case it would return a value to variable num.

To expand this means that execution reaches the line where I call the function, and should then skip down to where the function is written, and go through executing each line inside that function.

This is how it has worked for me previously when I use the F8 key to highlight and follow the code execution line by line.

The problem The problem is when execution reaches the line inside the sub procedure where the function is called the function is just skipped over and execution doesn't go inside the function and run each line inside it.

(and I should say when this happened I had 95% of a working program, and i've tried re-writing the function, calling other functions).

But whenever a function is called execution of the code doesn't go into the function itself it just skips over it, and the variable that holds the result of the function therefore is left empty.

I've tried creating breakpoints at the beginning of the function and when the function is called in the sub procedure however this hasn't worked.

At the very least execution should get to the definition of the function (Function FirstRow() etc) and throw and error but it's not doing that.

Sub Main

    Dim num as double

    Dim sheet_name as string

    num = FirstRow(sheet_name)

End Sub

Function FirstRow(sheet as string) as double

    select case sheet '<<----- execution never gets inside the function

    case "sheet"
        FirstRow = 8
    case "sheet2"
        FirstRow = 12

    end select

End Function

Aucun commentaire:

Enregistrer un commentaire