+ Reply to Thread
Results 1 to 4 of 4

Use variables defined in sub in my function

Hybrid View

niko79542 Use variables defined in sub... 02-11-2013, 11:47 AM
JosephP Re: Use variables defined in... 02-11-2013, 11:55 AM
Norie Re: Use variables defined in... 02-11-2013, 11:57 AM
RHCPgergo Re: Use variables defined in... 02-11-2013, 11:57 AM
  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    Use variables defined in sub in my function

    Hi,

    When I write a function, do I have to redefine my variables? What If i want to use a variable that I defined in my sub?

    For example:

    Sub test()
    
    Dim Wa as Worksheet
    Set Wa = Worksheets(2)
    
    End sub
    How do I use Wa in my function without redefining it?

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Use variables defined in sub in my function

    pass it to the function as an argument
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Use variables defined in sub in my function

    You could either declare Wa publicly.
    Dim Wa As Worksheet
    
    Sub test()
    Dim x As String
    
       Set Wa = Worksheets(2)
    
    
       x = MyFunction()
    
    End Sub
    
    Function MyFunction() As String
           MyFunction = Wa.Name
    End Function
    Or, which is probably much better, pass it as an argument to the function.
    Sub test()
    Dim Wa as Worksheet
    Dim x As String
       Set Wa = Worksheets(2)
    
        x = MyFunction(Wa)
    
    End Sub
    
    Function MyFunction(ws As Worksheet) As String
    
           MyFunction = ws.Name
    
    End Function
    If posting code please use code tags, see here.

  4. #4
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Use variables defined in sub in my function

    Hi niko,

    not sure what you mean. You don't have to redefine your variable, but you can assign different values to it later on. After you assigned the value "Worksheets(2)" to your variable, you can use it instead of it. Like this:
     Wa.Range("A1").Value
    is the same as
    Worksheets(2).Range("A1").Value
    If your want to change the value, you just make another "Set" statement. For example
     Set Wa = WorkSheets("Sheet1")
    edit: never mind, I completely misunderstood.

+ 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.6.0 RC 1