0

I am trying to learn excel vba and i am having an issue with a value not being returned from a function.

I have a spreadsheet and i look for data in it , in a specific column, from a specific line. I am trying to find the line that contains my data. When found, i want to return that line number to my main sub.

The function finds the data and the line number. MsgBox displays the right line number but my main sub does not seem to see it.

Below my simple function:

Function findLine()
Dim Line As Integer
Dim Output As Integer

Line = 4 
mysh = "data" 

Do Until Sheets(mysh).Range("B" & Line).Value = "found" And Sheets(mysh).Range("C" & Line).Value = "you"
   Line = Line + 1
Loop

Output = Line
MsgBox Output ' this displays the right line number 
End Function

and the call from my name :

myline = findLine()
Msgbox myline  ' this shows an empty box. 

Could you please advise about what i am doing wrong ?

cheers !

  • 5
    In VBA, you need to assign the return value to the function name. Put the statement `findLine = line` at the end of your code – FunThomas Jun 28 '22 at 10:03
  • But, to answer your question correctly, what you are doing wrong is not reading the manual/MS help pages. – freeflow Jun 28 '22 at 11:15

0 Answers0