I am trying to figure out how to display newly entered text from one userform text box to another userform text box in a new line below. Pretty much I want it to a multiline input. Here is the code I am using so far:
Private Sub CommandButton1_Click()
UserForm1.TextBox1.Value = TextBox1.Value & vbNewLine
End Sub
Try this
Private Sub CommandButton1_Click()
UserForm1.TextBox1.Value = UserForm1.TextBox1.Value & vbNewLine & UserForm2.TextBox1.Value
End Sub
Related
I have an Excel user form with two text boxes. When I try to enter a value in one of them, Excel freezes and stops working. But the strange thing is that this works on other computers in the office which are exactly the same.
It is a really simple user form with two text boxes, set to change, and one button.
I'm not sure if it's because of Excel, maybe something in Windows.
I have Excel 2016 and Windows 10 on my computer.
Has anyone had this problem?
Private Sub applyCommandButton_Click()
Range("A3").Value = TextBox1.Value & " " & TextBox2.Value
End Sub
Private Sub TextBox1_Change()
Range("A1").Value = TextBox1.Value
End Sub
Private Sub TextBox2_Change()
Range("A2").Value = TextBox2.Value
End Sub
I have created a UserForms with a comboBox (dropdown).
And i have also written sub in a module.
I want to call the userforms in the one the steps of the sub, like a Inputbox.
The userforms
I have written the a Public Sub UserForm_Initialize() in userforms code
Public Sub UserForm_Initialize()
Dim t
UserForm.Show
t = ComboBox1.Value
End Sub
My code in the module :
Sub Ingestion()
Dim x, z
Dim rRange
Sheets("Time Log").Select
x = Range("H300").End(xlUp).Row
If Range("A" & x).Value <> "" Then
z = InputBox("Please confirm the Task type")
Range("B" & x) = z
End If
End Sub
Now I want to change the Input box to the combobox.
but if i call this in the module, it throws an Error :
Sub Test()
Call UserForm_Initialize
End Sub
Sub or Function Not Defined
Basically, i am trying to create a Inputbox with drop down options.
A UserForm_Initialize event gets executed as soon as, surprise, the userform is initialized. So, trying to show the UF when it's already being opened is illogical. Next to that you'd have to refer to the correct UF name, in this case that would result in TaskList.Show.
You open the UF outside of the UF's code module. For example
Sub openUF()
TaskList.Show
End Sub
Then you can execute code inside the UF's module. These are usually bound to objects on the UF, e.g.:
Sub CommandButton_Click() 'in case the UF has a command button and a combobox
MsgBox "You picked '" & Me.ComboBox1.Value & "'.", vbInformation, "Help"
End Sub
I am experimenting with the capabilities of a User Form to assist in Data entry. I would like to know if there is a specific code that can transfer the Value of a Checkbox to a Text box that is on the same User Form when the box is checked. Basically, instead of having to type all of the words out, it would be easier to simply check a series of boxes to create the sentence. I know how to input the Checkbox value into the Excel worksheet but I have yet to figure out how to have that same value entered into a Text Box that would provide a "Preview" of the sentence for editing purposes and then the data can be transferred to the Excel Worksheet once it has been approved in the User Form. In my attached example that I had created and I have a before and the desired after result of what I am looking to do.
Thank you
JLY Test form:
EDIT:
Though really, you'd be best off using a listbox, much easier and shorter code. Put your list of items in a worksheet and set that to a dynamic named range so you can edit it and the userform will pick it up on the fly. Make sure the listbox has a MultiSelect property set to fmMultiSelectExtended and the ListStyle property is set to fmListStyleOption. Then you can select multiple entries in the listbox by holding the Ctrl key.
In this example if have put it in Sheet1 (though can be any sheet, and the sheet can even be hidden), and then set a dynamic named range named listProperties to this formula: =Sheet1!$A$2:INDEX(Sheet1!$A:$A,MAX(2,ROWS(Sheet1!$A:$A)-COUNTBLANK(Sheet1!$A:$A)))
Then the userform has this code:
Private Sub listJewelryProperties_Change()
Dim sTemp As String
Dim i As Long
For i = 0 To Me.listJewelryProperties.ListCount - 1
If Me.listJewelryProperties.Selected(i) = True Then sTemp = sTemp & " " & Me.listJewelryProperties.List(i)
Next i
Me.txtPreview.Text = WorksheetFunction.Trim(sTemp)
End Sub
Private Sub UserForm_Initialize()
Me.listJewelryProperties.Clear
Me.listJewelryProperties.List = ActiveWorkbook.Sheets("Sheet1").Range("listProperties").Value
End Sub
And this is what the results look like:
Original Answer Here for Posterity:
Alternate solution, set all your checkboxes to call a function and pass an argument of their value and caption, then the function will update the textbox. The reason for using the .Tag property is to avoid removing too much due to duplicates in partial matches for the checkboxes (such as Ring and Ring Band where just plain Ring can be found within Ring Band, this way it will only remove the Ring entry, and not incorrectly remove both Ring entries)
Private Sub chk14ktWhiteGold_Click()
UpdatePreview Me.chk14ktWhiteGold.Value, Me.chk14ktWhiteGold.Caption
End Sub
Private Sub chkAntique_Click()
UpdatePreview Me.chkAntique.Value, Me.chkAntique.Caption
End Sub
Private Sub chkArtisinal_Click()
UpdatePreview Me.chkArtisinal.Value, Me.chkArtisinal.Caption
End Sub
Private Sub chkBand_Click()
UpdatePreview Me.chkBand.Value, Me.chkBand.Caption
End Sub
Private Sub chkHandCarved_Click()
UpdatePreview Me.chkHandCarved.Value, Me.chkHandCarved.Caption
End Sub
Private Sub chkHandEtched_Click()
UpdatePreview Me.chkHandEtched.Value, Me.chkHandEtched.Caption
End Sub
Private Sub chkHandmade_Click()
UpdatePreview Me.chkHandmade.Value, Me.chkHandmade.Caption
End Sub
Private Sub chkRing_Click()
UpdatePreview Me.chkRing.Value, Me.chkRing.Caption
End Sub
Private Sub chkRingBand_Click()
UpdatePreview Me.chkRingBand.Value, Me.chkRingBand.Caption
End Sub
Private Sub chkSterlingSilver_Click()
UpdatePreview Me.chkSterlingSilver.Value, Me.chkSterlingSilver.Caption
End Sub
Private Sub chkVintage_Click()
UpdatePreview Me.chkVintage.Value, Me.chkVintage.Caption
End Sub
Private Sub UpdatePreview(ByVal bChkState As Boolean, ByVal arg_sText As String)
If bChkState = True Then
Me.txtPreview.Text = WorksheetFunction.Trim(Me.txtPreview.Text & " " & arg_sText)
If Len(Me.txtPreview.Tag) = 0 Then
Me.txtPreview.Tag = arg_sText
Else
Me.txtPreview.Tag = Me.txtPreview.Tag & "|" & arg_sText
End If
Else
Me.txtPreview.Tag = Replace("|" & Me.txtPreview.Tag & "|", "|" & arg_sText & "|", "|")
Me.txtPreview.Text = WorksheetFunction.Trim(Replace(Me.txtPreview.Tag, "|", " "))
End If
End Sub
Something like below will do that for you, as I don't know the names of your UserForm or your Checkboxes, you will have to amend as required, also you will have to add the following code to each of the CheckBoxes Click Event:
Private Sub CheckBox1_Click()
UserForm1.TextBox1.Text = UserForm1.TextBox1.Text & " " & CheckBox1.Caption
End Sub
UPDATE:
To also remove when checkbox is unchecked, the following code will do that:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
UserForm1.TextBox1.Text = UserForm1.TextBox1.Text & " " & CheckBox1.Caption
Else
pos = InStr(UserForm1.TextBox1.Text, CheckBox1.Caption)
If pos > 0 Then UserForm1.TextBox1.Text = Replace(UserForm1.TextBox1.Text, " " & CheckBox1.Caption, "")
End If
End Sub
I have read and applied solution I found on similar topics but nothing seem to work in my case.
So, I want to pass a variable from one sub of my Module1 to a userform. It's a string called "provinceSugg".
Here is the relevant part of my code :
Public provinceSugg As String
Sub probaCity()
[...]
If province = "" And city <> "" Then
provinceSugg = sCurrent.Cells(p, db_column).Offset(0, 1).Value
UserForm2.Label1 = "Do you mean " & city & " in " & provinceSugg & " ?"
UserForm2.Label1.TextAlign = fmTextAlignCenter
UserForm2.Show
Else
End If
End Sub
And then in my userform code :
Private Sub userformBtn1_Click()
MsgBox provinceSugg
sMain.Range("J6").Value = provinceSugg
End Sub
When I run my program :
1/ I have the content of provinceSugg showing in the MsgBox called from my sub (so there is a provinceSugg, it's not an empty variable).
2/ The MsgBox called from the userform is empty (so passing the value failed) and my program crashes when running " sMain.Range("J6").Value = provinceSugg" with something like "Error 424 Object Required" (so the variable failed to pass to the userform).
I tried all the stuff I found on forum and here (different ways to indicate that provinceSugg is a public variable but still crashing...).
Thanks in advance for your help !
You would be able to create public variables within the Userform that can be set by the Module.
These variables are only accessible within the Userform as it is loaded.
Within the Userform, declare public variables for both objects.
Public sMain As Worksheet
Public provinceSugg as string
Private Sub userformBtn1_Click()
MsgBox provinceSugg
sMain.Range("J6").Value = provinceSugg
End Sub
Within the Module, you can assess both of those variables.
Sub probaCity()
[...]
If province = "" And city <> "" Then
provinceSugg = sCurrent.Cells(p, db_column).Offset(0, 1).Value
With UserForm2
.provinceSugg = provinceSugg
Set .sMain = sMain
.Label1 = "Do you mean " & city & " in " & provinceSugg & " ?"
.Label1.TextAlign = fmTextAlignCenter
.Show
End With
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim selectColumn
selectColumn= Split(Target.Address(1, 0), "$")(0)
Call UserFormStart(selectColumn)
End Sub
Inside Main Module
Public columnSelection As String
...
Public Sub UserFormStart(ByVal columnRef As String)
'MsgBox "Debug columnRef=" & columnRef
columnSelection = columnRef
UserForm1.Show
End Sub
Inside UserForm
Private Sub UserForm_Initialize()
'MsgBox "Debug UserForm_Initialize =" & columnSelection
...
End Sub
Worksheet_SelectionChange calls a sub on the module where columnSelection is declared as public and visable from the UserForm.
I used three different variables for the Column Reference to show that there is where the UserForm has access to the Module.
The above all works and took ages to find and work out hence the submission. Happy hunting folks
If you have a hidden worksheet in your workbook, simply write the parameter to be passed to the User Form somewhere on the worksheet and go read it from there in the User Form.
I have a form with a number of text boxes for user input (this is in a User Form not on the spreadsheet). I have a few boxes that are related to currency and I need them to show the comma and decimal point as the user enters their criteria into the box. So far I have found a bunch of the same formulas online but when I input my number into the box it goes with 4.00 (if i hit 4 first) and all i can change after that is the second 0. Here is something similar I see online:
textbox1 = format(textbox1, "$#,##0.00")
Also seen some with cDbl
No matter what I try it won't let me enter anything more than the first number I enter. I need help. Thanks!
Formatting as the user types in data gets very tricky. May be better to format after the entry is complete.
Entry can also be validated and old value restored if entry deemed invalid
Dim TextBox1oldValue As String
Private Sub TextBox1_AfterUpdate()
If IsNumeric(TextBox1) Then
TextBox1 = Format(TextBox1, "$#,##0.00")
Else
TextBox1 = TextBox1oldValue
End If
End Sub
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(TextBox1) Then
TextBox1oldValue = Format(TextBox1, "$#,##0.00")
End If
End Sub
Private Sub UserForm_Initialize()
TextBox1oldValue = "$0.00"
TextBox1 = "$0.00"
End Sub
You need to use the TextBox Change event, like:
Private Sub TextBox1_Change()
If TextBox1 = vbNullString Then Exit Sub
If IsNumeric(TextBox1) Then CurrencyTransform(TextBox1)
End Sub
You then create the CurrencyTransform function to modify what it shows in the TextBox.
Try simply this...
Private sub textbox1_AfterUpdate()
textbox1 = format(textbox1, "$#,##0.00")
end sub
I wrote this inspired by chris' solution. It works while user is typing!
Private waiting As Boolean
Private Sub TextBox1_Change()
If waiting Then Exit Sub
waiting = True
TextBox1 = formatAsCurrency(TextBox1)
waiting = False
End Sub
Private Function formatAsCurrency(v As String)
If v = "" Then
formatAsCurrency = Format(0, "0.00")
Else
Dim vv As Variant
vv = Replace(v, ",", "")
vv = Replace(vv, ".", "")
formatAsCurrency = Format(vv / 100, "#,##0.00")
End If
End Function
Try this:
Private Sub TextBox1_Change()
TextBox1.Value = Format(TextBox1.Value, "$#,##0.00")
End Sub
This worked for me just fine, so it should help you as well.
If you want to do calculations that involve multiple text boxes, don't use .value after the name of the text box. Instead, use val( before the name of the text box while following it with an end parenthesis. I used .value and got weird results. Instead of, for example, $100 for TextBox1.Value + TextBox2.Value where TextBox1.Valueis equal to $25 and TextBox2.Value is equal to $75, I would get "$25$75".