+ Reply to Thread
Results 1 to 5 of 5

Thread: Returning value from a function to a sub procedure - Help Needed

  1. #1
    Registered User
    Join Date
    11-25-2011
    Location
    London England
    MS-Off Ver
    Excel 2010
    Posts
    15

    Returning value from a function to a sub procedure - Help Needed

    Guys,

    I have a problem, Chk out the code below. I have called function rowPosition from a sub procedure. I need to return the value of search text. But the value of searchtext in the sub procedure does not change. Can some one help me out

    Sub search()
    
    Dim searchtext As String
    searchtext = "Display diagonal"
    rowPosition searchtext
    MsgBox (searchtext)
    End Sub
    
    Function rowPosition(ByVal searchtext As Variant, _
                Optional ByVal stLookAt As XlLookAt = xlWhole) As Long
        
                Dim rPos As Long, rS As Range
        
                rPos = 0
        
                Set rS = Cells.Find(searchtext, LookIn:=xlValues, LookAt:=stLookAt)
        
                If Not rS Is Nothing Then rPos = rS.Row
        
                rowPosition = rPos
                MsgBox (rS.Row & "-" & rS.Column)
                MsgBox (Worksheets("sheet1").Cells(rS.Row, rS.Column + 1).Value)
                searchtext = Worksheets("sheet1").Cells(rS.Row, rS.Column + 1).Value
                MsgBox ("inside routine - " & searchtext)
                
                
    End Function
    Cheers,
    Ragav

  2. #2
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,957

    Re: Returning value from a function to a sub procedure - Help Needed

    A function should pass back a result and you should avoid using the same variable name internally for the function.

    So you should, in the Sub, declare a variable (let's say x, for the sake of argument) as Long and call your function thus:

    x=rowPosition(searchtext)

  3. #3
    Registered User
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2007
    Posts
    90

    Re: Returning value from a function to a sub procedure - Help Needed

    It is not clear on what you are trying to do. Generally functions do not change global variables. The searchstring in the function is only local to the function. Changing it there will not change searchstring in the sub.

    You function returns the row in which the first occurance of "Display diagonal" was found. Your message box in function is displaying the cell on the same row next to the cell where Display diagonal was found????? ---- Not clear on what you are trying to do.

    I am attaching a spreadsheet that fixes (my guess) your orignal code and I have add to other subs/function guessing what you were after... It does have some comments and may be useful in formulating what you were after. Be sure to enable macros and then press the buttons to activate the various subs.....

    Hope this helps,

    Ted
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,957

    Re: Returning value from a function to a sub procedure - Help Needed

    Quote Originally Posted by tkowal View Post
    It is not clear on what you are trying to do. Generally functions do not change global variables. The searchstring in the function is only local to the function. Changing it there will not change searchstring in the sub.
    Unless you change:

    Function rowPosition(ByVal searchtext As Variant, _
                Optional ByVal stLookAt As XlLookAt = xlWhole) As Long
    To:

    Function rowPosition(ByRef searchtext As Variant, _
                Optional ByVal stLookAt As XlLookAt = xlWhole) As Long
    Then changes to searchtext within the function will modify the value of the variable in the sub, but this isn't really how functions should be written.

  5. #5
    Registered User
    Join Date
    11-25-2011
    Location
    London England
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Returning value from a function to a sub procedure - Help Needed

    thanks a lot guys... It helped me out...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0