Show Sum of 2 Textboxes in 3rd Textbox - vba

I'm trying to do a direct calculation but it doesn't work.
This is what I have at the moment.
Private Sub TextBox270_AfterUpdate()
TextBox270.Value = Val(TextBox1.Value) + Val(TextBox150.Value)
End Sub
I am trying to achive
1 (in TextBox1) + 2 (in TextBox150) = 3 (in TextBox270)

You need to handle the change event of TextBox1 and Textbox150
Is this what you are trying? (Untested) I am assuming that you will be entering valid numbers in those two textboxes.
Private Sub TextBox1_Change()
GenerateSum
End Sub
Private Sub TextBox150_Change()
GenerateSum
End Sub
Sub GenerateSum()
If Len(Trim(TextBox1.Text)) <> 0 And _
Len(Trim(TextBox150.Text)) <> 0 Then
TextBox270.Text = Val(Trim(TextBox1.Text)) + _
Val(Trim(TextBox150.Text))
End If
End Sub

Related

Reiterating Sub TextBox1_Change() efficiently

Sub TextBox19_Change()
If Len(TextBox19.Value) = 4 Then TextBox19.Value = Mid(TextBox19.Value, 1, 3)
End Sub
Sub TextBox18_Change()
If Len(TextBox18.Value) = 4 Then TextBox18.Value = Mid(TextBox18.Value, 1, 3)
End Sub
Sub TextBox17_Change()
If Len(TextBox17.Value) = 4 Then TextBox17.Value = Mid(TextBox17.Value, 1, 3)
End Sub
Sub TextBox16_Change()
If Len(TextBox16.Value) = 4 Then TextBox16.Value = Mid(TextBox16.Value, 1, 3)
End Sub
How can I rephrase the above so that we don't have to duplicate the sub-routines for 100+ TextBoxes? There are more codes than just changing the textbox's value to its first 3 characters. I would appreciate a general efficient code. Thank you.
Edit: This isn't on a form. This is on PowerPoint Slides.
I wasn't sure if you're using form textboxes here, but you could also use event sinking.
So create a class clsCustomText like so
Private WithEvents t As msforms.TextBox
Private Const lMaxLength As Long = 3
Public Sub Init(tIn As msforms.TextBox)
Set t = tIn
End Sub
Private Sub t_Change()
If Len(t.Value) > lMaxLength Then t.Value = Left(t.Value, lMaxLength)
End Sub
Then in a normal module, somewhere to hold them
Public colCustomTextboxes As Collection
and then in the form like so, i did mine on click, so i can test. You'd need to move to initialize.
Private Sub UserForm_Click()
Dim c As Control
Dim t As clsCustomText
Set colCustomTextboxes = New Collection
For Each c In Me.Controls
If TypeOf c Is msforms.TextBox Then
Set t = New clsCustomText
t.Init c
colCustomTextboxes.Add t, c.Name
End If
Next c
End Sub

VBA UserForm ListBox and Data Table Writing

I am creating a UserForm for an Inventory Clerk who physically counts inventory for auditing purposes. The current process is on paper - I'd like to put it on a tablet.
Goal:
1) Single row comes up on form with item location, product, quantity, and description
2) If the quantity is correct, the user hits "correct" and the next item comes up
3) If the quantity is incorrect, the user keys the observed amount which gets written to column J of the corresponding data table
4) A scroll option to go forward and backward if the user wants to check/re-work an item
Private Sub CommandButton1_Click()
'GET DATA FROM TABLE
ListBox1.ColumnCount = 4
ListBox1.RowSource = "A2:D500"
End Sub
'IF QTY IS CORRECT, NEXT ROW
Private Sub CommandButton_QtyCorrect_Click()
Call SpinButton1_SpinUp
End Sub
'IF QTY DOESN'T MATCH, USER KEYS CORRECT
Private Sub CommandButton2_Click()
Range("K1") = TextBox2_Qty.Value
TextBox2_Qty.Value = ""
End Sub
Private Sub ListBox1_Click()
'DISPLAY DATA TABLE
End Sub
Private Sub SpinButton1_SpinDown()
If ListBox1.ListIndex > 0 Then
ListBox1.Selected(ListBox1.ListIndex - 1) = True
End If
End Sub
Private Sub SpinButton1_SpinUp()
If ListBox1.ListIndex + 1 < ListBox1.ListCount Then
ListBox1.Selected(ListBox1.ListIndex + 1) = True
End If
End Sub
Private Sub TextBox2_Qty_Change()
'USER OVERWRITE
End Sub
Questions:
1) With the current setup, all the rows populate the ListBox. How do I get one row at a time to display?
2) When the current row is displayed on the ListBox, how do I write to the corresponding row in column J in the case of a non-match?
Answers
1) Solved by Ralf S below.
2) Solution:
Private Sub AdjustButton_Click()
'IF QTY DOESN'T MATCH, USER KEYS CORRECT
Range("J" & SpinButton1.Value) = TextBox2_Qty.Value
TextBox2_Qty.Value = ""
End Sub
the following code would be my ansatz:
Private UserForm1_Activate()
ListBox1.ColumnCount = 4
ListBox1.RowSource = "A2:D2" ' show only first row
SpinButton1.Min = 2
SpinButton1.Max = Range("A1048576").end(xlUp).row ' last row as maximum value of spin button
End Sub
Private Sub CommandButton_QtyCorrect_Click()
If SpinButton1.Value < SpinButton1.Max Then _
SpinButton1.Value = SpinButton1.Value + 1
End Sub
Private Sub CommandButton2_Click()
Range("K1") = TextBox2_Qty.Value
TextBox2_Qty.Value = ""
End Sub
Private Sub SpinButton1_Change()
ListBox1.RowSource = "A" & SpinButton1.Value & ":D" & SpinButton1.Value
End Sub
Thus, you could use the spin button's value as the row index for your listbox.
This shows only one row at a time and it might help you out for now. But there is still a lot optimization potential...

ActiveX Combobox doesn't close automatically

I have an ActiveX Combobox in one of my main sheet which control/update a series of charts.
Private Sub cmBoxSelect_GotFocus()
Application.ScreenUpdating = False
With Me.cmBoxSelect
.List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
.ListRows = 6
.DropDown
End With
Application.ScreenUpdating = True
End Sub
Private Sub cmBoxSelect_Change()
'series of codes which manipulates the charts, based on selection...
End Sub
I noticed that when I click the ComboBox and select one of its content, it leaves a blue highlight on the selection. So to prevent that, I added:
Private Sub cmBoxSelect_DropButtonClick()
Application.ScreenUpdating = False
ActiveCell.Activate
Application.ScreenUpdating = True
End Sub
It successfully removed the highlight.
However, it has a weird drawback. cmbSelect doesn't close automatically once user didn't select anything (once the combobox is active and the user click any cell in the sheet, it doesn't close out). It was working before I added the DropButtonClick event.
Did I missed anything or any wrong steps above? Thanks for your inputs!
EDIT#1
Seems I already found a solution by trial and error. I only added a blank Label and select it to remove the focus out of the ComboBox whenever there is a change. I also changed the DropButtonClick to LostFocus.
Private Sub cmBoxSelect_GotFocus()
Application.ScreenUpdating = False
With Me.cmBoxSelect
.List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
.ListRows = 6
.DropDown
End With
Application.ScreenUpdating = True
End Sub
Private Sub cmBoxSelect_LostFocus()
ActiveCell.Select
End Sub
Private Sub cmBoxSelect_Change()
'series of codes which manipulates the charts, based on selection...
Me.Label1.Select
End Sub
You need to put the SelLength to 0 in multiple events to avoid highlighting:
so:
Me.cmBoxSelect.SelLength = 0
in:
Private Sub cmBoxSelect_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Private Sub cmBoxSelect_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Private Sub cmBoxSelect_LostFocus()
Private Sub cmBoxSelect_DropButtonClick()
Private Sub cmBoxSelect_Change()
Private Sub cmBoxSelect_GotFocus()
(you could add also Me.cmBoxSelect.SelStart = 0 )
Lets try this:
Not Event-triggered by a change, but by the dropbuttonclick
Private Sub changingComboBox(String s)
'series of codes which manipulates the charts, based on selection...
End Sub
Private Sub cmBoxSelect_DropButtonClick()
Dim s As String
s = cmBoxSelect.SelText
If (cmBoxSelect.SelText = cmBoxSelect.Value) Then
cmBoxSelect.Value = ""
cmBoxSelect.Value = s
Else
call changingComboBox(cmBoxSelect.Value)
End If
End Sub
How about that ?

VBA remove ListBox row on click

I have a list box not linked to any range.
I want to click on a row and remove it. If I step through the code below, the ListBox1_Click() function ends up being called twice for some reason and the application produces an "Unspecified Error" on the second run
My entire code:
Private Sub ListBox1_Click()
ListBox1.RemoveItem (ListBox1.ListIndex)
End Sub
Private Sub UserForm_Initialize()
For i = 0 To 10
ListBox1.AddItem ("A" & Str(i))
Next i
End Sub
If you make a button about it, then this solution would be quite ok there:
Private Sub CommandButton1_Click()
Dim cnt As Long
For cnt = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(cnt) Then
Me.ListBox1.RemoveItem cnt
Exit Sub
End If
Next cnt
End Sub

Making one set of code (with slight differences) apply to specific textboxes in userform

I have a UserForm where I have multiple rows of textboxes for data entry. Upon finishing entry for a specific textbox in each row, it triggers a macro for another textbox within the same row to be calculated.
The code is:
Private Sub TextBox46_Change()
If DataInput.TextBox46.Value > 0 And DataInput.TextBox46.Value < 1000 Then
DataInput.TextBox54.Value = 100 * ((DataInput.TextBox46.Value - DataInput.TextBox14.Value) / (DataInput.TextBox30.Value - DataInput.TextBox14.Value))
DataInput.TextBox62.Value = 100 - DataInput.TextBox54.Value
Else: MsgBox ("Revise Inputs")
End If
End Sub
Now I need to apply this for 8 other textboxes, except for example if it was TextBox47_Change then all the other textbox numbers in the code must shift up by 1. I have searched online and people have done it but the code did not change. Here,my code has slight differences for each textbox.
Is there a way to repeat this code without just copy and pasting it to each TextBox_Change sub and then changing the numbers.
For further clarification, in the image I have attached, everytime the Dish + Residue Mass column textbox changes then the TSR,VS,FS is calculated for the corresponding row.
Create a function that will return a reference to the textboxes based on a number.
Private Sub TextBox46_Change()
TextBox_ChangeEvent 0
End Sub
Private Sub TextBox47_Change()
TextBox_ChangeEvent 1
End Sub
Private Sub TextBox48_Change()
TextBox_ChangeEvent 2
End Sub
Private Sub TextBox49_Change()
TextBox_ChangeEvent 3
End Sub
Private Sub TextBox50_Change()
TextBox_ChangeEvent 4
End Sub
Private Sub TextBox51_Change()
TextBox_ChangeEvent 5
End Sub
Private Sub TextBox52_Change()
TextBox_ChangeEvent 6
End Sub
Private Sub TextBox53_Change()
TextBox_ChangeEvent 7
End Sub
Private Sub TextBox_ChangeEvent(Index As Integer)
If getTxtBox(46 + Index).Value > 0 And getTxtBox(46 + Index).Value < 1000 Then
getTxtBox(54 + Index).Value = 100 * ((getTxtBox(46 + Index).Value - getTxtBox(14 + Index).Value) / (getTxtBox(30 + Index).Value - getTxtBox(14 + Index).Value))
getTxtBox(62 + Index).Value = 100 - getTxtBox(54 + Index).Value
Else: MsgBox ("Revise Inputs")
End If
End Sub
Function getTxtBox(Index As Integer) As MSForms.TextBox
Set getTxtBox = DataInput.Controls("TextBox" & Index)
End Function