Return String with a form

  • The easiest way to do this is to create a Public variable. I suggest creating this variable in the location where you are trying to "pass" it to.


    For example, lets say this is the Module you wish to pass the string to, and this module is called "Module1":


    Module1 Code:


    Code
    'This should be put anywheres in the first lines of code in the module
    [CODE]Public MyVariable as String

    [/CODE]



    Userform code:


    Code
    'Wherever you exit the form, put this line
    Module1.MyVariable = "This is the string to pass"
  • Hi Stef,


    There are two ways.


    1. Instead of unloading the userform when the text entry has been made, hide it. You can then access the control values.


    For example:
    In the userform:

    Code
    Private Sub CommandButton1_Click()
        Me.Hide
    End Sub

    In a general module:

    Code
    Sub Test()
        Dim strTest As String
        
        UserForm1.Show
            
        strTest = UserForm1.TextBox1.Value
        MsgBox strTest
        
    End Sub

    2. Use a Public variable to hold the string value.


    For example:
    In the userform:

    Code
    Private Sub CommandButton1_Click()
    
    
        strTest = Me.TextBox1.Value
        
        Unload Me
        
    End Sub

    In a general module:

    HTH

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!