+ Reply to Thread
Results 1 to 5 of 5

Transfering Worksheet varaible from Userform to Module?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-07-2011
    Location
    netherlands
    MS-Off Ver
    Excel 2003
    Posts
    128

    Transfering Worksheet varaible from Userform to Module?

    I posted my problem as an excample.

    I want to set a worksheet variable in a userform and want to be able to use it in a module.


    Userform
    Public WS As Worksheet
    
    
    Private Sub TestButton_Click()
        Application.Run "Test1"
    End Sub
    
    Private Sub UserForm_initialize()
        Set WS = Blad7
    End Sub
    Macro Test1
     Sub Test1()
        WS.Cells(1, 1) = "TEST"
    End Sub

    When you set a variable in a module you can use it for an userform but not visa versa.

    Any one know how to fix this without setting the the WS again in de module?
    Last edited by vin1; 04-18-2011 at 07:34 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Transfering Worksheet varaible from Userform to Module?

    You could use:
    Sub Test1()
        Userform1.WS.Cells(1, 1) = "TEST"
    End Sub
    but it would be better to pass the worksheet directly to the sub:
    Private WS As Worksheet
    
    Private Sub TestButton_Click()
        Test1 WS
    End Sub
    
    Private Sub UserForm_initialize()
        Set WS = Blad7
    End Sub
    and:
    Sub Test1(wks as worksheet)
        wks.Cells(1, 1) = "TEST"
    End Sub
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor
    Join Date
    02-07-2011
    Location
    netherlands
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: Transfering Worksheet varaible from Userform to Module?

    It sounds logical and i think that might work but..


     Test1 WS
    This is for a subroutine within the same userform but I need to call a subroutine from a module should be like:

      Application.run "Test1"
    I tried a few changes with that code but without succes.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Transfering Worksheet varaible from Userform to Module?

    Quote Originally Posted by vin1 View Post
     Test1 WS
    This is for a subroutine within the same userform
    No it's not. Try it...

  5. #5
    Forum Contributor
    Join Date
    02-07-2011
    Location
    netherlands
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: Transfering Worksheet varaible from Userform to Module?

    ok Tnxs.

    wrong tested before thats why i tought it wasnt correct.


    Solved

+ 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