I have two userforms. In userform1 the script that sets the value of "x" is:
Private Sub Combobox1_Change()
x = Combobox1.Value + 2
End Sub
I need userform2 to initialize with textbox1.value = x from userform1.
I don't even know where to start on this one. Any insight on how to do this?
EDIT: I'm trying the code below for userform2:
Private Sub UserForm_Initialize()
x = userform1.combobox1.Value
textbox1.Value = x
End Sub
The userform initializes fine, but the textbox1.Value working.
You can get the value from the combobox directly. It will have the values even if it is not visible any more.
UserForm2:
Private Sub UserForm_Initialize()
Me.TextBox1.Value = UserForm1.ComboBox1.Value + 2
End Sub
I also wrote to test it:
In a Modul:
Sub test()
UserForm1.Show 'I just had a fix value in the combobox for the test
UserForm2.Show
End Sub
Related
Thank you in advance.
I have a UserForm1 that contains ListBox1. The ListBox1 shows the range of data from the excel sheet.
If I click on data from ListBox1, it pops up Userform2 with the corresponding data.
In essence, if the user wants to edit the data in ListBox1, they would double click on the data which pops up as UserForm2, and CommandButton1 as a saving function. This should then be displayed in the ListBox1 with the changes.
The issue I have is that if I edit the data in UserForm2 and click on the CommandButton1, it does not reflect in my ListBox1 and it gives me the error code, "Run-time error'70': Could not set the Column Property. Permission Denied.
The code I have for the ListBox1 is as follows:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
UserForm2.TextBox9.Text = Me.ListBox1.Column(0)
UserForm2.TextBox1.Text = Me.ListBox1.Column(1)
UserForm2.TextBox2.Text = Me.ListBox1.Column(2)
UserForm2.TextBox3.Text = Me.ListBox1.Column(3)
UserForm2.TextBox4.Text = Me.ListBox1.Column(4)
UserForm2.TextBox5.Text = Me.ListBox1.Column(5)
UserForm2.TextBox6.Text = Me.ListBox1.Column(6)
UserForm2.TextBox7.Text = Me.ListBox1.Column(7)
UserForm2.TextBox8.Text = Me.ListBox1.Column(8)
UserForm2.Show
End Sub
The code I have for CommandButton1 is as follows:
Private Sub CommandButton1_Click()
UserForm1.ListBox1.Column(0) = Me.TextBox9.Text
UserForm1.ListBox1.Column(1) = Me.TextBox1.Text
UserForm1.ListBox1.Column(2) = Me.TextBox2.Text
UserForm1.ListBox1.Column(3) = Me.TextBox3.Text
UserForm1.ListBox1.Column(4) = Me.TextBox4.Text
UserForm1.ListBox1.Column(5) = Me.TextBox5.Text
UserForm1.ListBox1.Column(6) = Me.TextBox6.Text
UserForm1.ListBox1.Column(7) = Me.TextBox7.Text
UserForm1.ListBox1.Column(8) = Me.TextBox8.Text
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
UserForm2.TextBox1.Value = Me.ListBox1.List(0)
UserForm2.TextBox2.Value = Me.ListBox1.List(1)
UserForm2.TextBox3.Value = Me.ListBox1.List(2)
UserForm2.Show
End Sub
Private Sub UserForm_Initialize()
For i = 1 To 3
Me.ListBox1.AddItem Cells(i, 1).Value
Next
End Sub
Private Sub CommandButton1_Click()
UserForm1.ListBox1.List(0) = Me.TextBox1.Value
UserForm1.ListBox1.List(1) = Me.TextBox2.Value
UserForm1.ListBox1.List(2) = Me.TextBox3.Value
End Sub
I need some help with getting the right code to do the following:
I have 4 groups of radio buttons inside a frame in a userform
Each group is a simple Yes/No radio button
I have a textbox that I want to autofill with a score range of A-D depending on the # of "yes" radio buttons selected.
The "No" checkboxes really shouldn't do anything in regards to the textbox
Userform Name = TP_UF
Frame Name = fun_opt_frame
Option Button Name for "Yes" = fun_score_yes1-4
Textbox Name = fun_scorebox
Logic:
4 Yesses = A
3 Yesses = B
2 Yesses = C
1 Yes = D
It doesn't matter what order the yesses are selected, its a total count. I tried using code using the frame but not sure if that is the best way. The frame for these radio buttons isn't needed for any reason other then to perhaps make it easier to code. So I could throw out the frame if it's not necessary to get this working.
I am not sure where to start here. Any help would be appreciated.
pic
The quickest and easiest way for you to understand is - I guess - the following code. You have to put the code into the class module of the userform.
Option Explicit
Dim opt1 As Byte
Dim opt2 As Byte
Dim opt3 As Byte
Dim opt4 As Byte
Private Sub opt1Yes_Click()
opt1 = 1
EvalOpt
End Sub
Private Sub opt1No_Click()
opt1 = 0
EvalOpt
End Sub
Private Sub opt2yes_Click()
opt2 = 1
EvalOpt
End Sub
Private Sub opt2No_Click()
opt2 = 0
EvalOpt
End Sub
Private Sub opt3yes_Click()
opt3 = 1
EvalOpt
End Sub
Private Sub opt3No_Click()
opt3 = 0
EvalOpt
End Sub
Private Sub opt4yes_Click()
opt4 = 1
EvalOpt
End Sub
Private Sub opt4No_Click()
opt4 = 0
EvalOpt
End Sub
Private Sub EvalOpt()
Dim sumOpt As Byte
Dim res As String
sumOpt = opt1 + opt2 + opt3 + opt4
Select Case sumOpt
Case 1: res = "D"
Case 2: res = "C"
Case 3: res = "B"
Case 4: res = "A"
Case Else: res = ""
End Select
Me.fun_scorebox.text = res
End Sub
I assumed the option buttons are named opt1Yes, opt1No, opt2Yes, opt2No etc.
A more advanced solution would probably be to use classe modules and "collect" the option buttons in such a way.
I ended up going about this differently and I got it working using a counter. Thanks for the help! Posting code here in case anyone else needs it.
Option Explicit
Private Sub OptionButton1_Change()
set_counter
End Sub
Private Sub OptionButton2_Change()
set_counter
End Sub
Private Sub OptionButton3_Change()
set_counter
End Sub
Private Sub OptionButton4_Change()
set_counter
End Sub
Private Sub OptionButton5_Change()
set_counter
End Sub
Private Sub OptionButton6_Change()
set_counter
End Sub
Private Sub OptionButton7_Change()
set_counter
End Sub
Private Sub OptionButton8_Change()
set_counter
End Sub
Private Sub set_counter()
Dim x As Integer, counter As Integer
Me.TextBox1.Value = ""
counter = 0
For x = 1 To 8 Step 2
If Me.Controls("OptionButton" & x).Value = True Then counter = counter + 1
Next x
Me.TextBox1.Value = Choose(counter, "D", "C", "B", "A")
End Sub
Private Sub UserForm_Activate()
Me.TextBox1.Value = ""
End Sub
Private Sub UserForm_Click()
Dim x As Integer
Me.TextBox1.Value = ""
For x = 1 To 8
Me.Controls("OptionButton" & x).Value = False
Next x
End Sub
I have a Problem with a Userform which I called "ComboTest2". It only consists of two Comboboxes. If I instantiate the USerform as an object then the following Code doesn't work in the sense that the second combobox of the Userform doesn't contain the desired data.
Sub FillCombo(ByVal row As Long)
Dim rgCities As Range
Set rgCities = Worksheets("Tabelle2").Range("B2:D2").Offset(row)
ComboTest2.ComboBox2.Clear
ComboTest2.ComboBox2.List = WorksheetFunction.Transpose(rgCities)
ComboTest2.ComboBox2.ListIndex = 0
End Sub
Sub FillMain()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
ComboForm2.Show
End Sub
UserForm-Code:
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Me.Hide
End Sub
Private Sub ComboBox1_Change()
FillCombo ComboBox1.ListIndex
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Worksheets("Tabelle2").Range("A2:A5").Value
ComboBox1.ListIndex = 0
FillCombo ComboBox1.ListIndex
End Sub
But if I use the "default instantiation" by VBA which means that I change the FillMain Sub to:
Sub FillMain2()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
'ComboForm2.Show
ComboTest2.Show
End Sub
Then everything is fine. Why is that so?
Best regards
It's because FillCombo is referring to the userform by name, and therefore to the default instance (you're actually creating a new instance of the form). If that code is not in the userform, and I'm not sure why you would have it outside the form, you should pass the combobox as an argument to it.
I have one UserForm with 1 TextBox and 1 ComboBox.
I firstly write in the ComboBox (per exemple Sarah)
Private Sub ComboBox1_Change()
ThisWorkbook.Sheets("CalculSheet").Range("A2").Value = ComboBox1.Text
End Sub
Then it makes some calcul in A3 like (If D2=Sarah Then D3=1)
Private Sub UserForm_Active()
Application.ScreenUpdating = False
ThisWorkbook.Worksheets("CalculSheet").Activate
TextBox1 = Range("A3").Value
End Sub
And I want that the Result comes directly in my TextBox1. It means that I write Sarah in the ComboBox1 and directly comes 1 in the TextBox1.
Delete the Private Sub UserForm_Active() code. You don't need that. Replace ComboBox1_Change() with this.
Is this what you are trying? (Untested)
Private Sub ComboBox1_Change()
With ThisWorkbook.Sheets("CalculSheet")
.Range("A2").Value = ComboBox1.Text
DoEvents
TextBox1.Text = .Range("A3").Value
End With
End Sub
I have a textbox on a userform. It is the only textbox on the form. There are three labels and two buttons in addition to this textbox. Basically, I want the focus to remain on this textbox under all scenarios, other than the moment that one of the buttons would be clicked, but then I want the focus to come right back to the text box. Both buttons have "TakeFocusOnClick" and "TabStop" set to False. I was having problems with getting the focus set to the textbox, which is why I changed these two settings.
Once I changed these settings, the Enter key in the textbox stopped having any effect. I have events written for _AfterUpdate and _KeyPress for the textbox, but they don't fire. As you can see in the code, I have commented out the lines to set the focus to this textbox. Since it is now the only object that can take focus, these lines are not needed (theoretically). When I allowed the other objects to take focus, these lines weren't having any effect (focus was switching to the buttons despite these SetFocus lines).
Here is the code. It is very simple, except that the Enter key isn't triggering the event. Can anyone see why? Thanks.
Private Sub btnDone_Click()
Application.Calculation = xlCalculationAutomatic
formMath.Hide
'Clear statistics
Range("attempts").Value = 0
Range("correct").Value = 0
Sheet5.Range("A2:W500").ClearContents
End Sub
Private Sub btnSubmit_Click()
recordAnswer
'formMath.txtAnswer.SetFocus
End Sub
Private Sub txtAnswer_AfterUpdate()
recordAnswer
'formMath.txtAnswer.SetFocus
End Sub
Private Sub txtAnswer_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then
recordAnswer
End If
End Sub
Private Sub UserForm_Initialize()
'Initialize manual calculation
Application.Calculation = xlCalculationManual
Application.Calculate
'Initialize statistics
Range("attempts").Value = 0
Range("correct").Value = 0
Sheet5.Range("A2:W500").ClearContents
'Initialize first problem
newProblem
End Sub
Sub recordAnswer()
'Update statistics
Dim attempts, correct As Integer
attempts = Range("attempts").Value
correct = Range("correct").Value
Range("results").Offset(attempts, 0).Value = attempts + 1
Range("results").Offset(attempts, 1).Value = lblTopNum.Caption
Range("results").Offset(attempts, 2).Value = lblBotNum.Caption
Range("results").Offset(attempts, 3).Value = lblBop.Caption
Range("results").Offset(attempts, 4).Value = Range("Answer").Value
Range("results").Offset(attempts, 5).Value = txtAnswer.Text
If (Range("Answer").Value = txtAnswer.Text) Then
Range("results").Offset(attempts, 6).Value = 1
Else
Range("results").Offset(attempts, 6).Value = 0
End If
'Update attempts and success
Range("attempts").Value = attempts + 1
Range("correct").Value = correct + 1
newProblem
End Sub
Sub newProblem()
Application.Calculate
formMath.lblTopNum.Caption = Range("TopNum").Value
formMath.lblBotNum.Caption = Range("BotNum").Value
formMath.lblBop.Caption = Range("ProbType").Value
formMath.txtAnswer.Value = ""
'formMath.txtAnswer.SetFocus
End Sub
To start off
You can either in the design mode, set the TabIndex property of the Textbox to 0 or you can set the focus on the textbox in the UserForm_Initialize()
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
Similarly after any operation that you perform, simply call the TextBox1.SetFocus to revert to the textbox.
Option Explicit
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
Private Sub CommandButton1_Click()
MsgBox "Hello from Button 1"
TextBox1.SetFocus
End Sub
Private Sub CommandButton2_Click()
MsgBox "Hello from Button 2"
TextBox1.SetFocus
End Sub
Let me know if this is not what you want?
I found a way to accomplish this. In the code above I took out the _KeyPress and _AfterUpdate events and replaced them with:
Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 13: recordAnswer
End Select
End Sub
Not sure why the other methods didn't work, but this does.
Also not sure why simply setting the focus directly didn't work. I suspect that the focus was being set, but then something else was happening subsequently that was changing the focus off of the textbox. Just a guess.
Thanks for the help. I appreciate it.