PDA

View Full Version : [SOLVED:] Userform TextBox - Accept variable and pass to my main macro



dj44
04-30-2017, 08:15 AM
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

Paul_Hossler
04-30-2017, 01:21 PM
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

dj44
04-30-2017, 02:12 PM
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

gmaxey
04-30-2017, 05:00 PM
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

Paul_Hossler
05-01-2017, 06:31 AM
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)

dj44
05-01-2017, 02:03 PM
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