Reputation: 59
I have userform1 with a textbox1 and space on the form is limited. I am trying to create an interactive/editable 'pop-up' to give the user a larger space to enter/edit text in textbox1. I created a mini userform2 with textbox2. I added a small commandbutton1 to userform1 that opens userform2, which works fine. I want textbox1 and textbox2 to mirror each other. As text is added/deleted/edited in one, the same occurs in the other. I thought this would be easy, but I can't even get text from textbox1 to appear in textbox2. Here is what I've tried
Private Sub commandbutton1_Click() 'userform1 module
Dim n As userform2
Set n = New userform2
n.Show 'works
userform2.textbox2.Text = userform1.textbox1.Text 'not working
End Sub
Private Sub Userform_Initialize() 'userform2 module
userform2.textbox2.Text = userform1.textbox1.Text 'not working
End Sub
Private Sub textbox2_Change() 'tried in userform1 and userform2 modules
userform1.textbox1.Text = userform2.textbox2.Text 'not working
End Sub
Private Sub textbox1_Change() 'tried in userform1 and userform2 modules
userform2.textbox2.Text = userform1.textbox1.Text 'not working
End Sub
EDITED
Userform1 contains textbox1 and commandbutton1
Userform2 contains textbox2
I was able to get textbox2 to match textbox1, but not the other way around. Also, I'm not able to keep userform2 in front. For example, if I 'touch' or 'click' on userform1, userform2 disappears behind userform1.
'code in userform1 module
Private Sub commandbutton1_Click()
Dim N As userform2
Set N = New userform2
N.Show
End Sub
Private Sub textbox1_Change() 'Works! If I type in textbox1
userform2.textbox2.Text = Me.textbox1.Text
End Sub
'code in userform 2 module. Not able to keep userform2 in front of userform1 if I 'touch' or 'click' on userform1
Private Sub UserForm_Initialize()
Me.textbox2.Text = userform1.textbox1.Text
Me.StartUpPosition = 0
Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Width)
Me.Top = Application.Top + (0.5 * Application.Height) - (0.5 * Height)
End Sub
Private Sub textbox2_Change() 'If I type in textbox2, it does NOT appear in textbox1
userform1.textbox1.Text = Me.textbox2.Text
End Sub
Upvotes: 0
Views: 99
Reputation: 718
I have added another answer here so that you can see the difference between the two.
You can force Userform2 to stay on top of all other userforms by setting the ShowModal
property to True
. You will need to provide a command button to close the form. You will also not be able to access any controls on Userform1, including textboxes, until you close Userform2. You probably need to reassess your requirements. Do you really need to be able to type in either textbox and have that text appear in the other immediately? Maybe a better approach would be to open Userform2 as modal with any text already entered in Userform1, allow the user to type whatever text they need, and then copy that text back to the textbox on Userform1 when you close Userform2. The code below will do that for you. You need to add a command button to Userform2 and name it pbCloseForm
. Due to your lack of screen real estate, I have also adjusted the Userform2 positioning code to open Userform2 to the right of the Textbox on Useform1.
Code for Userform1:
Option Explicit
Private Sub CommandButton1_Click()
UserForm2.Show
End Sub
Code for Userform2:
Option Explicit
Private Sub pbCloseForm_Click()
' Put Userform2 text back into Userform1
UserForm1.TextBox1.Value = UserForm2.TextBox2.Value
' Hide Userform2
UserForm2.Hide
End Sub
Private Sub UserForm_Activate()
' Copy Userform1 text to Userform2
UserForm2.TextBox2.Value = UserForm1.TextBox1.Value
' Position Userform2 to the right of Userform1 Text box
UserForm2.Left = UserForm1.Left + UserForm1.TextBox1.Left + UserForm1.TextBox1.Width
UserForm2.Top = UserForm1.Top + UserForm1.TextBox1.Top
End Sub
Upvotes: 0
Reputation: 718
The following code applies to two userforms, each containing a textbox. The user can type text into either userform/textbox and it will be reflected in the other userform/textbox. You can take this example and adapt it to your needs.
ThisWorkbook module (optional - used just to get the show on the road):
Option Explicit
Private Sub Workbook_Open()
Call UserForm1.Show
End Sub
Userform1 module code:
Option Explicit
Private Sub commandbutton1_Click()
' Dim N As UserForm2 >> Do not create a new instance
' Set N = New UserForm2 >> of Userform2
UserForm2.Show
End Sub
Private Sub textbox1_Change()
UserForm2.TextBox2.Text = Me.TextBox1.Text
End Sub
Userform2 module code:
Option Explicit
Private Sub UserForm_Initialize()
'Me.StartUpPosition = 0 >>Do NOT set this programmatically - set property at design time
' Position Userform2 to the right of Userform1
UserForm2.Left = UserForm1.Left + UserForm1.Width
UserForm2.Top = UserForm1.Top
End Sub
Private Sub textbox2_Change()
UserForm1.TextBox1.Text = Me.TextBox2.Text
End Sub
Userform1 and Userform2 need the following property set:
ShowModal: False
StartUpPosition: 0 - Manual
(optional for Userform1)
Textboxes in both userforms need the following properties set:
EnterKeyBehaviour: True
Multiline: True
Upvotes: 0