Change event code for two inter related text boxes - excel

I use an UserForm with 3 Text boxes.
TestBox1 for entering numeric values(LP).
TextBox2 for entring Discount in percentage and TextBox3 is for displaying the discounted value.
Now I want to get the Discount in percent if the user puts the discounted value in TextBox3.
l have tried Change event but both the Textbox values are changing.
Option Explicit
Dim Lp As Double
Dim Perc As Double
Private Sub Calculate()
Lp = CDbl(TextBox1.Value)
Perc = CDbl(TextBox2.Value)
TextBox3.Value = Lp - Lp * Perc / 100
End Sub
Private Sub TextBox2_Change()
TextBox2.Value = Lp - Lp * CDbl(TextBox1.Value) / 100
End Sub
Private Sub TextBox3_Change()
TextBox2.Value = (Lp - TextBox2.Value) * 100 / Lp
End Sub
Private Sub CommandButton1_Click()
Call Calculate
End Sub

Just use a procedure for calculation and run it in both change events of your textboxes.
Option Explicit
Private Sub TextBox1_Change()
CalculateResult
End Sub
Private Sub TextBox2_Change()
CalculateResult
End Sub
Private Sub CalculateResult()
If TextBox1.Value <> vbNullString And TextBox2.Value <> vbNullString Then
Textbox3.Value = TextBox1.Value * TextBox2.Value 'adjust your calculation
Else
Textbox3.Value = "fill box 1 and 2 first"
End If
End Sub

Related

Sub _change reference of added comobox to userform VBA

The following code will open a userform (examples) in which I have a commmandbutton which adds a new Combobox. I can name the combobox and can retrive the name in the immediate window, but I am unable to accecs the change command of the added combobox(es) or retrive the acces to their input.
In would like to do things upon change of this added combobox. I am able to do it for 1 box using the set "newgene1" = .... but than it on reclickit this first box will get lost.
Option Explicit
Dim Counter As Integer
Private WithEvents newgene1 As MSForms.ComboBox
Private WithEvents newgene2 As MSForms.ComboBox
Dim Generanger As Range
Private Sub UserForm_Initialize()
Set Generanger = ThisWorkbook.ActiveSheet.Range("A1", Range("A1").End(xlDown)) 'list of data
Generanger.Select
Counter = 1
cboGenelist.RowSource = Generanger.Address
End Sub
--------------------------------------------------------------------
Private Sub cboGenelist_Change()
MsgBox (cboGenelist.Text)
End Sub
--------------------------------------------------------------------
Private Sub cmdAddgene_Click()
UserForm1.Height = UserForm1.Height + 20
UserForm1.Controls.Add("Forms.comboBox.1", "newgene" & Counter) = "select"
With Me.Controls("newgene" & Counter)
.Left = 20
.Top = UserForm1.Height - 50
.RowSource = Generanger.Address
.Font.Name = "Trebuchet MS"
.Font.Size = 12
End With
Debug.Print Me.Controls("newgene" & Counter).Name
Counter = Counter + 1
End Sub
--------------------------------------------------------------------
Private Sub newgene1_Change()
MsgBox (newgene.Text)
End Sub
--------------------------------------------------------------------
Private Sub newgene2_Change()
MsgBox (newgene2.Text)
End Sub

Userform2.initialize where textbox1.value = combobox1.value from userform1

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

Create a VBA program that will perform an addition or subtraction of two Hexadecimal numbers

I'm trying to get both addition and subtraction options to work and display the proper symbol between each label box. I've been running into an error, which VBA highlights for me. The boxes A, B, and C need to be valued at 10, 11, and 12. When you click a box it's supposed to populate the first empty box with the value. When both boxes are full and you click another letter, its should not be changed, instead the values will be locked until you hit clear.
Current code:
Private Sub Addition_Click()
Me.Result = (Me.LblFirstNum + 0) + (Me.LblSecondNum + 0)
'lblresult = Val(LblFirstNum) + Val(LblSecondNum)
End Sub
Private Sub BtnA_Click()
With LblFirstNum
'to display text
.Caption = "10"
.TextAlign = fmTextAlignCenter
'wrap text
.WordWrap = True
.Font.Size = 18
End With
End Sub
Private Sub BtnB_Click()
With LblSecondNum
'to display text
.Caption = "11"
.TextAlign = fmTextAlignCenter
'wrap text
.WordWrap = True
.Font.Size = 18
End With
End Sub
Private Sub BtnC_Click()
With LblSecondNum
'to display text
.Caption = "12"
.TextAlign = fmTextAlignCenter
'wrap text
.WordWrap = True
.Font.Size = 18
End With
End Sub
Private Sub Calculate_Click()
'Me.Result = (Me.LblFirstNum + 0) + (Me.LblSecondNum + 0)
lblresult.Value = Val(LblFirstNum.Value) + Val(LblSecondNum.Value)
End Sub
Private Sub Clear_Click()
Unload UserForm1
UserForm1.Show
End Sub
Private Sub CommandButton2_Click()
'Exit Command
UserForm1.Hide
End Sub
Private Sub Result_Change()
End Sub
Private Sub Label4_Click()
End Sub
Private Sub LblFirstNum_Click()
End Sub
Private Sub LblSecondNum_Click()
End Sub
Private Sub LblSign_Click()
End Sub
Private Sub Subtraction_Click()
'Me.Result = (Me.LblFirstNum + 0) - (Me.LblSecondNum + 0)
lblresult.Value = Val(LblFirstNum.Value) - Val(LblSecondNum.Value)
End Sub
In Calculate_Click() and Subtraction_Click(), you've tried to retrieve the text shown on a label using LblFirstNum.Value and LblSecondNum.Value. Labels don't have a "Value" property. The correct property to use is "Caption".
In Addition_Click(), it's unclear whether Result is the correct name for the control you're trying to reference. Elsewhere in this code, you have lblresult so you should establish which is the correct name and use that throughout.
You may also want to check the use of Unload in Clear_Click() (assuming that UserForm1 is the form we are working with). Your current code will probably result in an automation error on the UserForm1.Show line

VBA Placeholder text that disappears only upon typing, not when entering textbox on UserFrom

' I'm looking to create placeholder text (ghosting text) to help users know what to type in the field, but I want it to act very similar to on-line forms where the placeholder text does not disappear upon entering a textbox, but only disappears if you type new text into it.
' enterfieldbehavior is set to 1 - fmEnterFieldBehaviorRecallSelection in properties to avoid selecting placeholder text
Private Sub userform_initialize()
TextBox2.Value = "Name" 'upon starting UserForm, the placeholder text is launched in the textbox
TextBox2.ForeColor = &H8000000C 'grey
End Sub
Private Sub TextBox2_Enter()
If TextBox2.Text <> "Name" Then
TextBox2.SelStart = TextBox2.SelLength 'Upon entering the textbox, the cursor is placed only at the start and not the middle or end of the placeholder text
Else
' I need the oppositie of the above, to put the cursor at the end of text as the placeholder text is gone
End If
End Sub
Private Sub TextBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
TextBox2.SelStart = TextBox2.SelLength ' If a user uses the mouse to enter the textbox
End Sub
Private Sub TextBox2_Change()
If TextBox2.Text <> "Name" Then
TextBox2.Text = ""
TextBox2.ForeColor = &H8000000C 'grey
Else
TextBox2.Value = TextBox2.Value ' This is where I'm lost as I want to earse the holder text, and let the user type whatever they want
TextBox2.ForeColor = vbBlack
End If
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox2.Text = "" Then
TextBox2.Text = "Name" ' If there are no changes to the textbox, replace the placeholder text
TextBox2.ForeColor = &H8000000C 'grey
Else
End If
End Sub
Here is how I would do it:
Private Sub Label1_Click()
TextBox1.SetFocus
End Sub
Private Sub TextBox1_Change()
Label1.Visible = Len(TextBox1.Text) = 0
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Label1.Visible = Len(TextBox1.Text) = 0
End Sub
Private Sub UserForm_Initialize()
With Label1
.SpecialEffect = fmSpecialEffectSunken
.BackColor = vbGrayText
.Caption = " Name"
End With
End Sub
The easiest solution in only 1 line of code is:
Duplicate (copy/paste) your TextBox2 and name it e.g. TextBox2PlaceHolder
Set its Text property to whatever placeholder value you want, eg. "YYYY/MM/DD"
Set its Enabled property to False so that text color will be light gray
Send it to the background just behind your original TextBox2 (same Left and Top properties)
And finally, the masterpiece of code below will just switch the TextBox2.BackStyle to transparent if no text exists and so will let the placeholder appear!
Private Sub TextBox2_Change()
TextBox2.BackStyle = IIf(Len(TextBox2.Text) = 0, fmBackStyleTransparent, fmBackStyleOpaque)
End Sub
Here's what that looks like:

Fill a textbox with varaible text depending on Radio Button selection combo (VBA Excel)

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

Resources