I'm trying to make a simple user form in Excel to enter the date that a staff member did a certain task. I have a spreadsheet that lists all of my staff down column B and I have a list of all the tasks along row 5.
I've created a form with a couple of combo boxes so I can select the staff member and also select the task. There is also a text box which I will type a date into.
What I need is when I click the "submit" button it will enter the date contained in the text box into the cell defined by the 2 selections in the combo boxes. If either of the combo boxes have nothing selected then no data should be entered into the spreadsheet when the button is clicked.
Dim nameRow As Integer
Dim job1col As Integer
nameRow = cbxStaffName.ListIndex + 6
job1col = cbxJob1.ListIndex + 3
ThisWorkbook.Sheets("Job Rotation").Cells(nameRow, job1col).Value = tbxDate.Value
The combo box containing the list of staff names is called cbxStaffName. The combobox containg the jobs is cbxJob1. The textbox containing the date I want to enter is named tbxDate.
The combo boxes have been populated with data that exists on the spreadsheet, simply names down the left and jobs along the top. If for example Jim did the job welding I want to select Jim from one combo box, select welding from the other box and when I click the button the date will go into that cell on the spreadsheet.
You would just need an If statement to check if both checkboxes have a value:
If cbxStaffName.Value <> vbNullString And cbxJob1.Value <> vbNullString Then
'write into cell
Else
MsgBox "Please select staff and job first"
End If
Alternatively you can check the ListIndex which should be -1 if nothing was selected.
Note that I recommend to always use Long instead of Integer especially when dealing with row counts. Excel has more rows than Integer can handle.
Related
I am using Microsoft Access at the moment.
I am trying to populate a number of combo boxes based on another combo box value. For example the first combo box is called cmb_BoxValue where it has a number of 1-20 and the remaining combo box will have the name cmb_Box1 until cmb_Box20. Once the user has selected the number of combo box required, using an AfterUpdate sub it will allow visibility = True based on the value. If the selected value is 3, cmb_Box1, cmb_Box2 and cmb_Box3 Visible = True. I managed to do it using a select case like below:
Private Sub cmb_BoxValue_AfterUpdate()
Dim Size1 As Integer
Size1 = Me.cmb_BoxValue.Value
Select Case Me.cmb_BoxValue
Case 1
Me.cmb_boxBox1 = True
etc...
But I feel like this is very repetitive and not the most ideal solution. I feel like a for loop is the ideal solution but I am not sure how to approach it. Thank you
Since comboboxes have similar names with number suffix, could dynamically build combobox name in an incrementing index loop, with a test if index is <= cmb_BoxValue input to set Visible property:
For x = 1 to 20
Me.Controls("cmb_Box" & x).Visible = x <= Me.cmb_BoxValue
Next
I can upload a file if someone tells me how.
Need help replicating a filter as you type on a combo box that is being used at the record level. Example: Instead of having an open text box for the prefix (e.g. Mr. Mrs. Ms. Dr.), I'm using a combo box that looks up from a reference table. I want to be able to type the letter "r" in the combo box and have it filter out Ms. and showing the remaining values. Once I make a selection store the selected value in the Name table.
Issue: When I add a new value in Combo4 the other rows above clear out if they don't match the value I just typed into the cell. Something likely with the RowSource in the below formula. Do I have something out of sequence or a flawed formula?
What I think I'm trying to do:
1) If Prefix value populated w/ value in t_Name THEN show the matching value in t_ref_Prefix
2) If Combo4 is Blank / Null THEN then open Combo4 and show all values in t_ref_Prefix so a value can be selected.
3) If user is typing text into Combo4 THEN filter on change using * on both sides of the typed value.
Option Compare Database
Option Explicit
Private Sub Combo4_Change()
'https://stackoverflow.com/questions/48133260/display-records-in-access-db-combobox-on-any-text-typed-by-user
'test number of characters entered - if greater then 0 then assign rowsource
If Len(Me.Combo4.Text) > 0 Then
'set the rowsource to match user search criteria
Me.Combo4.RowSource = "SELECT * FROM t_ref_Prefix WHERE Prefix LIKE '*" & Me.Combo4.Text & "*'"
'show the search in real-time
Me.Combo4.Dropdown
Else
'set to no
Me.Combo4.RowSource = "SELECT t_ref_Prefix.auto, t_ref_Prefix.prefix, t_ref_Prefix.sort FROM _
t_ref_Prefix ORDER BY t_ref_Prefix.sort, t_ref_Prefix.prefix"
End If
End Sub
You need to set
Combo4.AutoExpand = False
This will do it.
I want to create a Combo box (Combo Box B) on an Access form containing numbers 1-10. In other words, the drop-down shows numbers 1-10 sequentially.
However, what is shown in the drop-down is dependent on Combo box A.
If x shows in Combo Box A, items 1-10 should show in Combo Box B.
If y shows in Combo Box A, numbers 1-5 should only show in Combo Box B, or at minimum prevent someone from selecting 6 or larger.
If z shows in Combo Box A, nothing should be selectable in Combo Box B.
My coding skills are rusty as I've not done much in over 10 years. Is this something easily achievable in Access, or will I need some VBA to assist?
Assuming that your question isn't just a contrived example, and that you really want to display the integers 1-10 and 1-5 in your combobox, then in it's simplest form, you could use the following function evaluated on the After Update event of your combobox 'A' and on the On Load event of your form:
Function UpdateComboRowSource(cmbCom As ComboBox, ByVal strVal As String)
cmbCom.RowSourceType = "Value List"
Select Case LCase(strVal)
Case "x": cmbCom.RowSource = "1;2;3;4;5;6;7;8;9;10"
Case "y": cmbCom.RowSource = "1;2;3;4;5"
Case Else: cmbCom.RowSource = ""
End Select
End Function
Private Sub Form_Load()
UpdateComboRowSource ComboB, ComboA.Value
End Sub
Private Sub ComboA_AfterUpdate()
UpdateComboRowSource ComboB, ComboA.Value
End Sub
I have added a ComboBox to a form, no additional formatting so far. I have a text box that the user enters a number of competitors (value) between 20 and 100 into. I want to populate the ComboBox so that the user can select a competitor from 1 to 100 in the ComboBox. So the user will be able to click the drop-down menu and select a competitor from a list of competitors, for example Competitor 1 to Competitor 100.
Please let me know if you need any extra info.
Try this, explanations included:
yourComboBox.Items.Clear() 'to make sure the ComboBox is empty before populating and to avoid duplicating records
If Val(yourTextbox.Text) > 0 Then 'basic checking
For x = 1 To Val(yourTextbox.Text) 'loop from 1 up to the value you entered into the textbox
yourComboBox.Items.Add(x) 'add the value of x which holds the current loop number/competitor
Next
End If
I have a form for work that is auto populated from a pre-existing customer form, but the drop downs and I are disagreeing. Due to this form being for work and being used/transferred through multiple organizations, I do not want any drop down boxes. What I am looking at doing is to have the selected values from the drop down boxes auto populate into the corresponding cells within the new form. ie: The drop down is in OriginalSheet D12 and I want the selected value to populate in NewSheet D12.
I have tried all of these answers:
Get dropdown value in VBA and get the name of the dropdown...nowhere to be found?
Return the text from a dropdown box rather than the index number
Return the selected text from a dropdown box
and am currently on the below code, but I still cannot get the selected value to populate into the new cell:
Sub Dropdown()
Dim dd As Dropdown
Set dd = Sheets("LTL Quote Form").Dropdowns("Drop Down 63")
Set r = Sheets("Sheet3").Range("D12")
Set ddValue = r(dd.ListIndex)
End Sub
Try this:
Sub Dropdown()
With ActiveSheet.DropDowns(Application.Caller)
Sheets("Sheet3").[d12] = .List(.Value)
End With
End Sub
Note: this assumes you have assigned the Dropdown() macro to the dropdown form control.
Note: this assumes that you want the value to appear in cell D12 of 'Sheet3'.