Consulting

Results 1 to 6 of 6

Thread: Userform TextBox - Accept variable and pass to my main macro

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Userform TextBox - Accept variable and pass to my main macro

    Hi folks,

    Good Sunday

    How do i get my macro to accept the userform text box value that I enter.

    I know how to use a simple input box but the input box is very small and I can't often read the value in that case I wanted to use a userform textbox but oh dear unfortunately I've got stuck and I'm not sure how to make this textbox accept my macro submit click value if that makes any sense

    Sub ShowUserformTextBox()
    
    UF1.show
    
    'MsgBox "hello" & UF1.TextBox1.Value
    
    
    End Sub
    
    
    
     
        Sub ExampleTextBox_Input()
     
    
        
        Dim oText As String
        
        UF1.show
      
        ' -I know how to use an input box but i would like to use a userform bix as its bigger
      
        'oText = InputBox("Duplicate Range - how Many Times would you like to ?")
        'If Len(oText) = 0 Then
        'Exit Sub
        'End If
        
        
        '=====     HOW DO I get the value from the userform text box?
        
        oText = UF1.TextBox1.Value
     
     
        Dim i As Integer
        Selection.WholeStory
        Selection.Copy
        For i = 1 To oText
             Selection.PasteAndFormat (wdFormatOriginalFormatting)
        Next
     
    End Sub


    I am trying to pass a variable to my main macro and I have used an example

    please do have a look and see how I can achieve this task thank you so much
    Attached Files Attached Files
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    The easiest way --

    In the standard module


    Option Explicit
    
    Public sInput As String     `  <<<<<<<<<<<<<<<<<<<<<<<        
    
    Sub ShowUserformTextBox()
        UF1.show
        MsgBox "hello -- " & sInput
    
    End Sub

    and in the UF code module


    Option Explicit
    
    Private Sub CommandButton1_Click()
        sInput = UF1.TextBox1.Value
    
        Me.Hide
    
        Unload Me
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello Paul
    I must say this is spectacularly fabulous as I just spent the whole day stressing about this rather un appetizing input box
    you see the problem is the text is too small and I can't see what the text is saying even though I just input the values

    so I did some research and I came across one of Greg's user forms that was a faux input box simulation but then i lost the link to that
    so I couldn't find it so that upset me

    but I knew that there was a way to get the text box to accept the input but well I couldn't find anything else

    I am happy to report that I managed to get the input from the text box to be passed to the variable in the example macro shown so happy days

    thanks for the great tips and have a good Sunday all and paul

    thanks again
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    You can also make the variable public in the form:

    Option Explicit
    Sub ShowForm()
      UserForm1.Show
      MsgBox UserForm1.strInput
    End Sub
    Option Explicit
    Public strInput As String
    Private Sub CommandButton1_Click()
      strInput = TextBox1
      Hide
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    A little more advanced (but more elegant IMHO) is to have the value returned by a UF function


    Standard Module:


    Option Explicit
    
    Sub ShowUserformTextBox()
    
        Load UF1
    
        MsgBox "hello " & UF1.GetUserInput  ' <<<<<<<<<<<<<<<<<<
    
    End Sub


    Userform module:

    Option Explicit
    
    Function GetUserInput() As String
        Me.Show
        
        GetUserInput = UF1.TextBox1.Value
    
    End Function
    Private Sub CommandButton1_Click()
        Me.Hide
    End Sub
    I learned that from one of the guys in the Excel forum (pretty sure)
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Thank you Greg and Paul for the additional coding I have put it in my modules.

    I have also made a large text box and a white userform background to make a nice-looking input box.

    I made the font size extra large so my eyes are very happy with that

    I was very surprised to discover that there was only the standard input box that was tiny and the font was so tiny even a cat would squint at the screen as well it was a bit of necessary evil to use the input box


    But now I made myself a large white text box on a userform so I am happy
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

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