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...
Related
The request: Include a check box in my userform that gets checked/unchecked depending on True/False value in column of table
Current state:
I have a user form that is used to add edit or delete entries in a table.
Specifically the editing works in a way that I have a combo box that corresponds with the mail addresses in column A and text boxes for each column of the table.
When you select a mail address in the combo box the text boxes of the user form get filled in with the data from that row.
For the example I would like to add a check box that says "married" and corresponds to a column in the table that has "True" or "False" values. This check box should now be checked/unchecked automatically depending on the value in the corresponding column.
My code:
Private Sub c_01_Change()
' c_01 is the combo box
With c_01
' B_02 is the delete button
' B_03 is the save button
B_02.Visible = .ListIndex > -1
B_03.Visible = True
If .ListIndex = -1 Then Exit Sub
' T_0X are the text boxes
For j = 0 To UBound(.List, 2)
Me("T_" & Format(j, "00")).Text = .Column(j)
Me("T_" & Format(j, "00")).Locked = False
If j > 2 Then Me("T_" & Format(j, "00")).Text = Format(.Column(j), "0.00")
Next
End With
End Sub
Private Sub T_00_Change()
M_text 0
B_02.Visible = T_00.Text <> ""
End Sub
Private Sub T_01_Change()
M_text 1
End Sub
Private Sub T_02_Change()
M_text 2
End Sub
Private Sub T_03_Change()
M_text 3
End Sub
Private Sub T_04_Change()
M_text 4
End Sub
Sub M_text(y)
If c_01.ListIndex > -1 Then c_01.Column(y) = Me("T_" & Format(y, "00")).Text
End Sub
Add a checkbox.
Then check the cell for the value. In my example it is Cells(1,1):
Write this:
CheckBox1.Value = CBool(Cells(1, 1))
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
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
This is my first post on this forum. I have a quick question regarding VBA, Userforms with a listbox. My goal is to select two options and return a list of names in a listbox. I have attached the example userform and the example table that I would be choosing from. Any help would be appreciated.
Worksheet
Current VBA for Userform
Private Sub ListBox1_Click()
Sheets("Trainers1").Range("I2") = ListBox1
End Sub
Private Sub ListBox2_Click()
Sheets("Trainers1").Range("I2") = ListBox2
End Sub
Private Sub ListBox3_Click()
Sheets("Trainers1").Range("I3") = ListBox3
End Sub
Private Sub ListBox4_Change()
.ListBox4 = Sheets("Trainers1").Range("K2:K10")
End Sub
Private Sub UserForm_Initialize()
Dim cnt
Dim cntr As Integer
cntr = Application.WorksheetFunction.CountA(Sheets("Shift Pattern Key").Range("A:A"))
cnt = Application.WorksheetFunction.CountA(Sheets("Training Ratio").Range("A:A"))
For i = 2 To cntr
ListBox2.AddItem Sheets("Shift Pattern Key").Cells(i, 1)
Next i
For i2 = 2 To cnt
ListBox3.AddItem Sheets("Training Ratio").Cells(i2, 1)
Next i2
End Sub
You could iterate through the rows of your table and compare the values in each row to the values selected. If both values in a row match the values selected by the user you can then use the .AddItem method to add the name of the employee to the list.
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